我做了一个有多种选择的问答游戏。我从数据库中获得问题和多项选择答案。从数据库中检索并检查了正确的答案。我使用的数据库是sqlite数据库。
我的异常代码:
11-03 17:43:11.108: E/AndroidRuntime(640): FATAL EXCEPTION: main
11-03 17:43:11.108: E/AndroidRuntime(640): java.lang.RuntimeException: Unable to start activity ComponentInfo{priscillia.benkyo/priscillia.benkyo.TestLevel1}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
11-03 17:43:11.108: E/AndroidRuntime(640): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-03 17:43:11.108: E/AndroidRuntime(640): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-03 17:43:11.108: E/AndroidRuntime(640): at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-03 17:43:11.108: E/AndroidRuntime(640): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-03 17:43:11.108: E/AndroidRuntime(640): at android.os.Handler.dispatchMessage(Handler.java:99)
11-03 17:43:11.108: E/AndroidRuntime(640): at android.os.Looper.loop(Looper.java:137)
11-03 17:43:11.108: E/AndroidRuntime(640): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-03 17:43:11.108: E/AndroidRuntime(640): at java.lang.reflect.Method.invokeNative(Native Method)
11-03 17:43:11.108: E/AndroidRuntime(640): at java.lang.reflect.Method.invoke(Method.java:511)
11-03 17:43:11.108: E/AndroidRuntime(640): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-03 17:43:11.108: E/AndroidRuntime(640): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-03 17:43:11.108: E/AndroidRuntime(640): at dalvik.system.NativeStart.main(Native Method)
11-03 17:43:11.108: E/AndroidRuntime(640): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
11-03 17:43:11.108: E/AndroidRuntime(640): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
11-03 17:43:11.108: E/AndroidRuntime(640): at java.util.ArrayList.get(ArrayList.java:304)
11-03 17:43:11.108: E/AndroidRuntime(640): at priscillia.benkyo.TestLevel1.onCreate(TestLevel1.java:46)
11-03 17:43:11.108: E/AndroidRuntime(640): at android.app.Activity.performCreate(Activity.java:5008)
11-03 17:43:11.108: E/AndroidRuntime(640): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-03 17:43:11.108: E/AndroidRuntime(640): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
TestLevel1.java
public class TestLevel1 extends Activity {
List<Question> quesList;
int score=0;
int qid;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc, rdd;
Button butNext;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_level1);
DatabaseHelper db=new DatabaseHelper(this);
quesList=db.getAllQuestions();
currentQ=quesList.get(qid);
txtQuestion=(TextView)findViewById(R.id.soal);
rda=(RadioButton)findViewById(R.id.radio0);
rdb=(RadioButton)findViewById(R.id.radio1);
rdc=(RadioButton)findViewById(R.id.radio2);
rdd=(RadioButton)findViewById(R.id.radio3);
butNext=(Button)findViewById(R.id.lanjutBtn);
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.GetCorr_Ans()+" "+answer.getText());
if(currentQ.GetCorr_Ans().equals(answer.getText()))
{
score++;
Log.d("score", "Your score"+score);
}
if(qid<5){
currentQ=quesList.get(qid);
setQuestionView();
}else{
Intent intent = new Intent(TestLevel1.this, Result.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
finish();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.test_level1, menu);
return true;
}
private void setQuestionView()
{
txtQuestion.setText(currentQ.GetSoal_Desc());
rda.setText(currentQ.GetPilihan_A());
rdb.setText(currentQ.GetPilihan_B());
rdc.setText(currentQ.GetPilihan_C());
rdd.setText(currentQ.GetPilihan_D());
qid++;
}
}
请帮我解决这个问题。
最佳答案
我猜数据库没有返回任何内容:
quesList=db.getAllQuestions();
因此,您获得第一项(索引0)的下一行失败:
currentQ=quesList.get(qid);
您应该在获取第一个元素之前测试列表中是否有任何项目:
if (quesList.size() > 0) {
// you can now get items
}