2017-02-03 6 views
-3

У меня есть к неустранимым:Как отладить этот FATAL EXCEPTION Android?

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 о том, как использовать трассировку стека для отладки, но я не могу его использовать для моей ситуации. Можете ли вы помочь мне отладить это? Благодаря

+0

Пожалуйста, добавьте код своей деятельности 'com.example.tung.flagquiz.playing'. –

ответ

1

Если вы попытаться прочитать это, вы увидите это в StackTrace:

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

Это реальное исключение, которое вызвало приложение к сбою. Это означает, что вы пытаетесь получить доступ к элементу с индексом = 0, когда ArrayList пуст. Если вы продолжите читать StackTrace построчно вы заметите кулак строку, указывающий на ваш код:

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

Так исключение появляется в строке 95 playing.java.

0

В LogCat отчетливо видно ошибка, как следовать

Вызванный: java.lang.IndexOutOfBoundsException: Invalid индекс 0, размер составляет 0 в java.util.ArrayList.throwIndexOutOfBoundsException (ArrayList.java:255) at java.util.ArrayList.get (ArrayList.java:308) at com.example.tung.flagquiz.playing.showQuestion (play.java:95) at com.example.tung .flagquiz.playing.onResume (playing.j AVA: 67)

Ошибка происходит из-за линии @ 95 в Playing.java внутри метода showQuestion
Возможно, вы пытаетесь получить элемент с индексом, где ваш индекс> list.size