启动bufferedReader后,Android应用程序停止工作

时间:2015-04-18 17:00:33

标签: java android

我在Android Java编程方面遇到了一些问题。

这是我的情况:

  • 我正在尝试从文本文件加载文本行。
  • 我会将每一行作为一个单独的字符串变量 保存在ArrayList
  • 运行应用程序没有问题,但是当我运行时它失败了 按下将发送变量的按钮发送到的方法 应用程序将使用每行作为单独的问题。
  • 按下手机上的按钮后整个屏幕冻结。

我不知道如何解决这个问题。此外,我无法从logcat获得错误,非常奇怪。

我是编程的新手,但我尽我所能,所以任何帮助都表示赞赏。如果您不理解或想要更多信息,请询问和平。我已经修复了我得到的解决方案,但我仍然遇到代码问题,我不知道如何修复它。

以下是代码:

   public void levelOne(View v)throws IOException{
    Intent intent = new Intent(this, pageBeforeAction.class);
    Button buttond = (Button) findViewById(R.id.buttonOne);
    createQuestions("hogskoleprovet.txt");
    startActivity(intent);
}

public void levelTwo(View v) throws IOException{
    Intent intent = new Intent(this, pageBeforeAction.class);
    Button buttonC = (Button) findViewById(R.id.buttonTwo);
    createQuestions("hogskoleprovet.txt");
    startActivity(intent);

}

public void createQuestions(String hogskoleprovet) throws IOException{


    InputStream iS = getResources().getAssets().open(hogskoleprovet);
    BufferedReader reader = new BufferedReader(new InputStreamReader(iS));

    String question = reader.readLine();
    String answer = reader.readLine();
    String answerOne = reader.readLine();
    String answerTwo = reader.readLine();
    String answerThree = reader.readLine();
    String answerFour = reader.readLine();       

    while(reader != null){
        question = reader.readLine();
        answer = reader.readLine();
        answerOne = reader.readLine();
        answerTwo = reader.readLine();
        answerThree = reader.readLine();
        answerFour = reader.readLine();
        Question q = new Question (question, answer, answerOne, answerTwo, answerThree, answerFour);
        mQuestions.add(q);    break;        }reader.close();    }

这是我的logcat的输出:

 26837-26837/com.example.arnpet.ultimatehogskoleprovet E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.arnpet.ultimatehogskoleprovet, PID: 26837
java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:3969)
        at android.view.View.performClick(View.java:4637)
        at android.view.View$PerformClick.run(View.java:19422)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5479)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at android.view.View$1.onClick(View.java:3964) at android.view.View.performClick(View.java:4637)

1 个答案:

答案 0 :(得分:1)

你陷入无限循环,在这里:

while (reader != null)

reader的值永远不会在任何地方设置为null,因此您会陷入循环中。

相关问题