2015-04-18 50 views
-1

我在Android Java编程中遇到了一些问题。Android应用程序在发布bufferedRead后停止工作

这是我的情况:

  • 我想从一个文本文件中加载文本行。
  • 我会将每一行作为一个单独的字符串变量,将保存在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) 

+0

from your code,执行BufferedReader reader = new BufferedReader(new InputStreamReader(iS));''你需要输入问题,答案,answerOne,answerTwo,answerThree和answerFour的值。你在做那个吗? – Lal

+0

我认为我正在那样做。值将根据数组确定。现在我终于得到了日志猫的错误: –

+0

以及错误是什么?请您发布Logcat – Lal

回答

1

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

while (reader != null) 

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

+0

但我已经将读者等同为null,为什么它应该是一个无限循环呢? –