2017-10-06 178 views
-3

我正在開發測驗應用程序,用戶可以在特定時間內解決測驗,但是當我在仿真程序上運行應用程序時,它運行時沒有錯誤,但在真實設備上運行應用程序時會引發以下錯誤。在真實設備上運行Android IndexOutOfBoundsException

錯誤: -

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2683) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2744) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6195) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764) 
    Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 
    at java.util.ArrayList.get(ArrayList.java:411) 
    at com.gaurav.javascripttutorial.subactivities.QuizMainActivity.onCreate(QuizMainActivity.java:52) 

代碼: -

public class QuizMainActivity extends AppCompatActivity { 

    private ProgressBar countDownTimer; 
    private TextView timer; 
    public int counter; 

    List<Question> quesList; 
    int score=0; 
    int qid=0; 
    Question currentQ; 
    TextView txtQuestion; 
    RadioButton rda, rdb, rdc; 
    Button butNext; 

    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.quiz_main_activity); 

     /** 
     * Quiz coding 
     */ 

     DbHelper db=new DbHelper(this); 
     quesList=db.getAllQuestions(); 
     currentQ=quesList.get(qid); 
     txtQuestion=(TextView)findViewById(R.id.textView1); 
     rda=(RadioButton)findViewById(R.id.radio0); 
     rdb=(RadioButton)findViewById(R.id.radio1); 
     rdc=(RadioButton)findViewById(R.id.radio2); 
     butNext=(Button)findViewById(R.id.nextButton); 
     setQuestionView(); 
     butNext.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1); 
       RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId()); 
       Log.d("yourans", currentQ.getANSWER()+" "+answer.getText()); 
       if(currentQ.getANSWER().equals(answer.getText())) 
       { 
        score++; 
        Log.d("score", "Your score"+score); 
       } 
       if(qid<22){ 
        currentQ=quesList.get(qid); 
        setQuestionView(); 
       }else{ 
        Intent intent = new Intent(QuizMainActivity.this, QuizResultActivity.class); 
        Bundle b = new Bundle(); 
        b.putInt("score", score); //Your score 
        intent.putExtras(b); //Put your score to your next Intent 
        startActivity(intent); 
        finish(); 
       } 
      } 
     }); 

     /** 
     * Timer Coding 
     */ 
     countDownTimer = (ProgressBar) findViewById(R.id.progressBar); 
     timer = (TextView) findViewById(R.id.timer); 

     new CountDownTimer(1200000,1000){ 

      @Override 
      public void onTick(long millisUntilFinished) { 
       int progress = (int) (millisUntilFinished/120000)+1; 
       countDownTimer.setProgress(countDownTimer.getMax()-progress); 
       if (counter == 1080){ 
        Toast.makeText(QuizMainActivity.this, "Only 2 minutes remaining, Please review the answers.", Toast.LENGTH_SHORT).show(); 
       } 
       counter++; 
       Log.d("Counter number", ""+counter); 
      } 

      @Override 
      public void onFinish() { 
       Toast.makeText(QuizMainActivity.this, "Sorry! Time is over, Try again", Toast.LENGTH_LONG).show(); 
       finish(); 
      } 
     }.start(); 
    } 

    @Override 
    public void onBackPressed() { 
     super.onBackPressed(); 
     finish(); 
    } 

    private void setQuestionView() 
    { 
     txtQuestion.setText(currentQ.getQUESTION()); 
     rda.setText(currentQ.getOPTA()); 
     rdb.setText(currentQ.getOPTB()); 
     rdc.setText(currentQ.getOPTC()); 
     qid++; 
    } 
} 

當程序運行和錯誤引發光標表示到下面的行的代碼: -

currentQ=quesList.get(qid); 
+7

錯誤說你的quesList是一個空的列表,你試圖從它得到第零個索引。 –

+0

你的數據庫是空的。首先在數據庫中放入一些問題 – Omer

+0

但是模擬器上的代碼工作相同,問題也成功添加到數據庫中。 –

回答

-1

創建列表後,您必須使用它的構造函數來實現它。因此,在使用您的列表之前添加以下行:

quesList = new ArrayList<>(); 
0

請檢查gelAllQuestions()函數。首先如果函數沒有返回空列表,你應該檢查null。之後,您可以檢查isEmpty()函數是否爲空列表。

請確定數據庫中有可用的數據。

相關問題