【问题标题】:Android Studio - Why does a string array cause my program to stop?Android Studio - 为什么字符串数组会导致我的程序停止?
【发布时间】:2015-04-06 23:51:12
【问题描述】:

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

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

public void 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 堆栈跟踪:stackoverflow.com/questions/23353173/…
  • 我无法使用 AVD,因为我使用的是 Windows 10,而 HAX 不适用于 Windows 10,所以我正在使用手机进行开发。
  • 首先,避免创建一千个字符串。 ((Button) this.findViewById(R.id.wakeButton)).setText(controlString [1]); 无需创建新的字符串对象即可工作。其次,如果你是从手机开发,去市场下载一个 logcat。然后检查你的错误
  • 好的,所以我烤了异常,我得到了 Length = 1; index=4 我明白它在说什么,但我仍然不明白为什么。当我祝酒结果时,我得到 x$x$x$x$ 就像我期望的那样,似乎拆分可能不起作用?
  • “我不能使用 AVD,因为我在 Windows 10 上,而 HAX 不能在 Windows 10 上工作,所以我正在用我的手机进行开发。”——所以?使用 LogCat 检查与您的崩溃相关的 Java 堆栈跟踪:stackoverflow.com/questions/23353173/… 如果您能够从手机上的 IDE 运行您的应用程序,则可以在手机上检查 LogCat。

标签: android arrays file-io crash android-widget


【解决方案1】:

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

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

我不得不使用这个:

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

【讨论】:

  • pattern.quote() 到底是什么?为什么有必要让拆分工作?
  • split() 将正则表达式作为其参数,如the documentation 中所述。 $ 是正则表达式中的特殊字符,表示与行尾匹配,如the documentation 中所述。如果您想从字面上拆分 $ 字符,可以在 Pattern 上使用 quote() 进行转义。
猜你喜欢
  • 1970-01-01
  • 2011-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-01
  • 2016-01-20
相关资源
最近更新 更多