2014-05-15 57 views
0

我創建一個測驗應用程序,它做了兩個問題,當應用程序需要經歷5個問題後崩潰。我不知道爲什麼,我跟着一個教程的基本下跌。這裏是QuizActivity的Android SQLite的測驗崩潰時,應該以後5

public class QuizActivity extends Activity 
{ 
List<Question> quesList; 
int score=0; 
int qid=0; 
Question currentQ; 
TextView txtQuestion; 
RadioButton rda, rdb, rdc; 
Button butNext; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_quiz); 
    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.button1); 
    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()); 
      if(currentQ.getANSWER().equals(answer.getText())) 
      { 
       score++; 
      } 
       currentQ=quesList.get(qid); 
       setQuestionView(); 
       if(qid<5) 
       { 
        currentQ=quesList.get(qid); 
        setQuestionView(); 
       } 
       else 
       { 
        Intent intent = new Intent(QuizActivity.this, ResultActivity.class); 
        Bundle b = new Bundle(); 
        b.putInt("score", score); //Your score 
        intent.putExtras(b); //Put your score to your next Intent 
        startActivity(intent); 
        finish(); 
       } 
     } 

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

} 

它推動通過對ResultsActivity

public class ResultActivity extends Activity 
{ 
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_result); 
    //get rating bar object 
    RatingBar bar=(RatingBar)findViewById(R.id.ratingBar1); 
    //get text view 
    TextView t=(TextView)findViewById(R.id.textResult); 
    //get score 
    Bundle b = getIntent().getExtras(); 
    int score= b.getInt("score"); 
    //display score 
    bar.setRating(score); 
    switch (score) 
    { 
    case 1: 
    case 2: t.setText("Oopsie! Better Luck Next Time!"); 
    break; 
    case 3: 
    case 4:t.setText("Hmmmm.. Someone's been reading a lot of trivia"); 
    break; 
    case 5:t.setText("Who are you? A trivia wizard???"); 
    break; 
    } 
} 
} 

和這裏是logcat的錯誤

10月5日至15日:50:04.686:W/dalvikvm(12197 ):threadid = 1:線程退出時未捕獲的異常(group = 0x41f758b0) 05-15 10:50:04.696:E/AndroidRuntime(12197):致命例外:main 05-15 10:50:04.696:E/AndroidRuntime (12197):java.lang。 IndexOutOfBoundsException:索引4無效,大小爲4 05-15 10:50:04.696:E/AndroidRuntime(12197):在java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 05-15 10:50:04.696 :E/AndroidRuntime(12197):在java.util.ArrayList.get(ArrayList.java:308) 10月5日至15日:50:04.696:E/AndroidRuntime(12197):在1 com.example.timestableseasy.QuizActivity $。 onClick(QuizActivity.java:50) 05-15 10:50:04.696:E/AndroidRuntime(12197):at android.view.View.performClick(View.java:4421) 05-15 10:50:04.696: E/AndroidRuntime(12197):在android.view.View $ PerformClick.run(View.java:17903) 十月5日至15日:50:04.696:E/AndroidRuntime(12197):在android.os.Handler.handleCallback( Handler.java:730) 十月5日至15日:50:04.696:E/AndroidRuntime(12197):在android.os.Handler.dispatchMessage(Handler.java :92) 05-15 10:50:04.696:E/AndroidRuntime(12197):at android.os.Looper.loop(Looper.java:213) 05-15 10:50:04.696:E/AndroidRuntime(12197) ):at android.app.ActivityThread.main(ActivityThread.java:5225) 05-15 10:50:04.696:E/AndroidRuntime(12197):at java.lang.reflect.Method.invokeNative(Native Method) 05 -15 10:50:04.696:E/AndroidRuntime(12197):在java.lang.reflect.Method.invoke(Method.java:525) 十月5日至15日:50:04.696:E/AndroidRuntime(12197):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:741) 05-15 10:50:04.696:E/AndroidRuntime(12197):at com.android.internal.os.ZygoteInit.main( ZygoteInit.java:557) 05-15 10:50:04.696:E/AndroidRuntime(12197):at dalvik.system.NativeStart.main(Native Method)

回答

1

你對你的Button點擊作爲的qid增加每次2的結果調用setQuestionView();兩次單擊的Button而不是1

+0

的是,爲什麼它崩潰的是,單獨的問題? –

+0

是的,這就是它崩潰的原因,你應該只調用一次函數,因爲它會增加你的'qid'或者改變你增加'qid'的地方 – Apoorv

1

指數從0開始,所以有4個元素將指數從0到3.訪問索引4 List超出範圍:

currentQ=quesList.get(qid); 

你需要確保qid< 4,或更多一般來說,< quesList.size()

爲什麼你在兩個問題後達到極限是因爲你爲每個問題增加了兩次qid

+0

由於已停止崩潰的 –