如何调试这个FATAL EXCEPTION Android?

时间:2017-02-03 11:07:38

标签: android indexoutofboundsexception

我有一个致命的例外:

FATAL EXCEPTION: main
Process: com.example.tung.flagquiz, PID: 2685
    java.lang.RuntimeException: Unable to resume activity {com.example.tung.flagquiz/com.example.tung.flagquiz.playing}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3409)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3440)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2774)
        at android.app.ActivityThread.access$900(ActivityThread.java:177)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5910)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
    Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
        at java.util.ArrayList.get(ArrayList.java:308)
        at com.example.tung.flagquiz.playing.showQuestion(playing.java:95)
        at com.example.tung.flagquiz.playing.onResume(playing.java:67)
        at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1269)
        at android.app.Activity.performResume(Activity.java:6297)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3398)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3440) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2774) 
        at android.app.ActivityThread.access$900(ActivityThread.java:177) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5910) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 

以下是我的游戏活动代码:

public class playing extends AppCompatActivity implements View.OnClickListener {
PopupWindow popup;
LayoutInflater layoutInflater;
ProgressBar progressBar;
final static long INTERVAL = 1000;
final static long TIMEOUT = 5000;
int progressValue = 0;
CountDownTimer mCountDown;
List<Question> questionPlay = new ArrayList<>();
DbHelper db;
int index=0,thisQuestion=0,totalQuestion,correctAnswer;
ImageView imageView;
Button btnA,btnB,btnC,btnD;
TextView txtScore,txtQuestion;
Intent intent = new Intent(this,finish.class);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_playing);
    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    txtScore = (TextView)findViewById(R.id.txtScore);
    txtQuestion = (TextView)findViewById(R.id.txtQuestion);
    btnA=(Button)findViewById(R.id.btnAnswerA);
    btnB=(Button)findViewById(R.id.btnAnswerB);
    btnC=(Button)findViewById(R.id.btnAnswerC);
    btnD=(Button)findViewById(R.id.btnAnswerD);
    btnA.setOnClickListener(this);
    btnB.setOnClickListener(this);
    btnC.setOnClickListener(this);
    btnD.setOnClickListener(this);

}
@Override
protected void onResume() {
    super.onResume();


    totalQuestion = 232;
    showQuestion(index);

    mCountDown = new CountDownTimer(TIMEOUT,INTERVAL) {
        @Override
        public void onTick(long millisUntilFinished) {
            progressBar.setProgress(progressValue);
            progressValue++;
        }

        @Override
        public void onFinish() {
            mCountDown.cancel();
            Bundle dataSend = new Bundle();
            dataSend.putInt("SCORE",correctAnswer);
            intent.putExtras(dataSend);
            startActivity(intent);
            finish();

        }
    };

}
private void showQuestion(int index) {
    if (index < totalQuestion) {
        thisQuestion++;
        txtQuestion.setText(String.format("%d/%d", thisQuestion, totalQuestion));
        progressBar.setProgress(0);
        progressValue = 0;
        int ImageId = this.getResources().getIdentifier(questionPlay.get(index).getImage().toLowerCase(), "drawable", getPackageName());
        imageView.setBackgroundResource(ImageId);
        btnA.setText(questionPlay.get(index).getAnswerA().toUpperCase());
        btnB.setText(questionPlay.get(index).getAnswerB().toUpperCase());
        btnC.setText(questionPlay.get(index).getAnswerC().toUpperCase());
        btnD.setText(questionPlay.get(index).getAnswerD().toUpperCase());

        mCountDown.start();
    } else {
        Bundle dataSend = new Bundle();
        dataSend.putInt("SCORE", correctAnswer);
        intent.putExtras(dataSend);
        startActivity(intent);
        finish();
    }
}
@Override
public void onClick(View v) {

    mCountDown.cancel();
    if (index < totalQuestion) {
        Button clickedButton = (Button) v;
        if (clickedButton.getText().equals(questionPlay.get(index).getCorrectAnswer().toUpperCase())) {
            correctAnswer++;
            showQuestion(++index);
        } else {
            Bundle dataSend = new Bundle();
            dataSend.putInt("SCORE", correctAnswer);
            intent.putExtras(dataSend);
            startActivity(intent);
            finish();

        }
    }
}}

我在Stackoverflow上读过一篇关于如何使用stacktrace进行调试的文章,但我不能将它用于我的情况。你能帮我解决这个问题吗?感谢

2 个答案:

答案 0 :(得分:1)

如果您尝试阅读此内容,您将在stacktrace中看到:

Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

这是导致应用程序失败的真正异常。这意味着,当ArrayList为空时,您尝试访问index = 0的元素。如果您逐行继续阅读stacktrace,您会注意到指向代码的第一行:

at com.example.tung.flagquiz.playing.showQuestion(playing.java:95)

所以异常出现在playing.java的第95行。

答案 1 :(得分:0)

在logcat中,显然可见错误如下

  

引起:java.lang.IndexOutOfBoundsException:索引0,大小无效   是0                                                                            在   java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)                                                                            at java.util.ArrayList.get(ArrayList.java:308)                                                                            在com.example.tung.flagquiz.playing.showQuestion(playing.java:95)                                                                            在com.example.tung.flagquiz.playing.onResume(playing.java:67)

错误是由于方法showQuestion 中的Playing.java中的@ 95行引起的 您可能正在尝试使用索引&gt; list.size

获取索引的项目