我一直在这个工作了一段时间,我即将拉出我的头发! 如果我用这个...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();
}
}
}
“我的程序崩溃” - 使用LogCat检查与您的崩溃相关的Java堆栈跟踪:https://stackoverflow.com/questions/23353173/uncomfort-myapp-has-stopped-how-can-i-solve-这 – CommonsWare
我不能使用AVD,因为我在Windows 10和HAX不适用于Windows 10,所以我正在开发我的手机。 – CJG
首先,避免创建一千个字符串。 '((Button)this.findViewById(R.id.wakeButton))。setText(controlString [1]);'不需要创建新的字符串对象。其次,如果您是从手机开发,请到市场并下载logcat。然后检查你的错误 – jb15613