You are calling setQuestionView();
twice on your Button
click as a result of which qid
is increased by 2 each time you click the Button
instead of 1
Android SQLite quiz crashes after two questions when it should be doing five
Domanda
i am creating a quiz app and it crashes after doing two questions when the app should be going through 5 questions. i'm not sure why as i followed a tutorial to get the basics down. here is the QuizActivity
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++;
}
}
it pushes through to the 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;
}
}
}
and here is the logcat errors
05-15 10:50:04.686: W/dalvikvm(12197): threadid=1: thread exiting with uncaught exception (group=0x41f758b0) 05-15 10:50:04.696: E/AndroidRuntime(12197): FATAL EXCEPTION: main 05-15 10:50:04.696: E/AndroidRuntime(12197): java.lang.IndexOutOfBoundsException: Invalid index 4, size is 4 05-15 10:50:04.696: E/AndroidRuntime(12197): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 05-15 10:50:04.696: E/AndroidRuntime(12197): at java.util.ArrayList.get(ArrayList.java:308) 05-15 10:50:04.696: E/AndroidRuntime(12197): at com.example.timestableseasy.QuizActivity$1.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): at android.view.View$PerformClick.run(View.java:17903) 05-15 10:50:04.696: E/AndroidRuntime(12197): at android.os.Handler.handleCallback(Handler.java:730) 05-15 10:50:04.696: E/AndroidRuntime(12197): at 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): at java.lang.reflect.Method.invoke(Method.java:525) 05-15 10:50:04.696: E/AndroidRuntime(12197): at 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)
Soluzione
Altri suggerimenti
Indices start from 0, so a List
with 4 elements will have indices from 0 to 3. Accessing index 4 is out of bounds:
currentQ=quesList.get(qid);
you need to make sure qid
is < 4
, or more generally, < quesList.size()
.
Why you hit the limit after two questions is because you're incrementing qid
twice for each question.