2015-04-06 52 views
-1

我一直在这个工作了一段时间,我即将拉出我的头发! 如果我用这个...Android Studio - 为什么字符串数组会导致我的程序停止?

public void readFile() { 

     BufferedReader buffReader = null; 
     StringBuilder result = new StringBuilder(); 
     try { 
      FileInputStream fileIn = openFileInput("VariableStore.txt"); 
      buffReader = new BufferedReader(new InputStreamReader(fileIn)); 
      String line; 
      while ((line = buffReader.readLine()) != null) { 
       result.append(line); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 

      try { 
       assert buffReader != null; 
       buffReader.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

     String resultString = result.toString(); 
     String[] controlString = resultString.split("$"); 
//  String wb = controlString[4]; 
//  String sb = controlString[5]; 

      ((Button) this.findViewById(R.id.wakeButton)).setText(resultString); 
//  ((Button) this.findViewById(R.id.sleepButton)).setText(sb); 
//  ((Button)this.findViewById(R.id.wakeButton)).setText(result); 
//  ((Button)this.findViewById(R.id.wakeButton)).setText(result); 
//  ((Button)this.findViewById(R.id.wakeButton)).setText(result); 

    } 

的Button.setText正常工作与“resultString”或“结果”,这是一个字符串,我必须输入格式为XXX XXX $ $ $ XXX XXX XXX $所以当我用readFile()将它读回来,并将其放入数组“controlString”,然后将数组元素分配给我的小部件,例如setText(controlString [0]);但如果我甚至取消注释行字符串wb = controlString [4];或者String sb = controlString [5];我的程序崩溃了。为什么阵列elemts在这里工作? 这里是我的WriteFile()......(这完美的作品

公共无效的WriteFile(){

BufferedWriter buffWriter = null; 
String wb = ((Button)this.findViewById(R.id.wakeButton)).getText().toString(); 
String sb = ((Button)this.findViewById(R.id.sleepButton)).getText().toString(); 
String tb = ((EditText)this.findViewById(R.id.textHoursBetween)).getText().toString(); 
String ti = ((EditText)this.findViewById(R.id.textIncrementTime)).getText().toString(); 
String td = ((EditText)this.findViewById(R.id.textIncrementDays)).getText().toString(); 

String writeString = wb + "$" + sb + "$" + tb + "$" + ti + "$" + td; 

try { 
    FileOutputStream fileOut = openFileOutput("VariableStore.txt", Context.MODE_PRIVATE); 
    buffWriter = new BufferedWriter(new OutputStreamWriter(fileOut)); 
    try { 
     buffWriter.write(writeString); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} catch (FileNotFoundException e) { 
    e.printStackTrace(); 
}finally { 

    try { 
     assert buffWriter != null; 
     buffWriter.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

}

+2

“我的程序崩溃” - 使用LogCat检查与您的崩溃相关的Java堆栈跟踪:https://stackoverflow.com/questions/23353173/uncomfort-myapp-has-stopped-how-can-i-solve-这 – CommonsWare

+0

我不能使用AVD,因为我在Windows 10和HAX不适用于Windows 10,所以我正在开发我的手机。 – CJG

+1

首先,避免创建一千个字符串。 '((Button)this.findViewById(R.id.wakeButton))。setText(controlString [1]);'不需要创建新的字符串对象。其次,如果您是从手机开发,请到市场并下载logcat。然后检查你的错误 – jb15613

回答

-1

我发现这个问题... 取而代之的是:

String[] controlString = resultString.split("$"); 

我不得不用这样的:

String[] controlString = resultString.split(Pattern.quote("$")); 
+0

无论如何,pattern.quote()到底是什么?为什么有必要让Split工作? – CJG

+0

'split()'将一个正则表达式作为其参数,如[文档]中所述(http://developer.android.com/reference/java/lang/String.html#split%28java.lang.String %29)。 '$'是正则表达式中的特殊字符,表示与行尾相匹配,如[documentation](http://developer.android.com/reference/java/util/regex/Pattern)中所述。 HTML)。如果你想从字面上分割一个'$'字符,你可以使用'Pattern'上的'quote()'来转义。 – CommonsWare

相关问题