Android Studio - 为什么字符串数组会导致我的程序停止?

标签 android arrays file-io crash android-widget

我已经为此工作了一段时间了,我快要拔掉我的头发了! 如果我用这个...

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”或“result”配合使用,“result”是我输入的格式为 xxx$xxx$xxx$xxx$xxx 的字符串,因此当我使用 readFile() 读回它时,我想要使用 .Split 并将其放入数组“controlString”中,然后将数组元素分配给我的小部件,即 setText(controlString[0]);但如果我取消注释 String wb = controlString[4]; 行或 String sb = controlString[5];我的程序崩溃了。为什么数组元素在这里不起作用? 这是我的 writeFile()...(效果很好。

公共(public)无效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();
    }
}

}

最佳答案

我发现问题了... 而不是这个:

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

我必须使用这个:

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

关于Android Studio - 为什么字符串数组会导致我的程序停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29481497/

相关文章:

c - 效率 : arrays vs pointers

python - 与 numpy repeat 一起使用的两个数组的逐元素编织

Android使用HTTP多部分表单数据上传视频到远程服务器

android - 在 RelativeLayout 中使用 ImageView 重复图像

java - 当我将 SimpleDateFormat 类与 "YYYY"一起使用时,应用程序崩溃

c++ - Qt从文件中删除一行

python - Appengine 上的 file.read() 返回垃圾字符

android - 如何从 Android 应用发出 Google App Engine 身份验证请求?

c++ - find_if 找到一个不等于某物的元素

javascript - 使用 node.js 扫描/跟踪许多文件并跳过已处理的文件