I've experienced some error in my program. I'm trying to make quiz app using radiobutton. The question and answer option is obtained from database.
This is some part of my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* Get Intent Parameter */
/* Open Database File */
db_helper.openDataBase();
db = db_helper.getReadableDatabase();
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.setContentView(R.layout.activity_exercise_question);
Qcontent = (TextView) findViewById(R.id.txtQuestion);
Qnum = (TextView) findViewById(R.id.txtNumb);
a = (RadioButton) findViewById(R.id.rbA);
b = (RadioButton) findViewById(R.id.rbB);
c = (RadioButton) findViewById(R.id.rbC);
d = (RadioButton) findViewById(R.id.rbD);
g = (RadioGroup) findViewById(R.id.rgOption);
next = (Button) findViewById(R.id.btnNextQuestion);
next.setBackgroundColor(Color.GREEN);
//To handle event when button Next is clicked
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int radioButtonID = g.getCheckedRadioButtonId();
View radioButton = g.findViewById(radioButtonID);
int idx = g.indexOfChild(radioButton);
if (opsys.getString(idx + 1).equals(opsys.getString(5))) {
nilai = nilai + 1;
}
quiz();
}
});
quiz();
}
public void finish() {
next.setText("Finish");
next.setBackgroundColor(Color.RED);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
int radioButtonID = g.getCheckedRadioButtonId();
View radioButton = g.findViewById(radioButtonID);
int idx = g.indexOfChild(radioButton);
if (opsys.getString(idx + 1).equals(opsys.getString(5))) {
score = score + 1;
}
ExerciseQuestion.this.finish();
Intent showScore = new Intent(ExerciseQuestion.this, ExerciseScore.class);
String achieveScore = String.valueOf(score);
Bundle bundle = new Bundle();
bundle.putString("AchieveScore", achieveScore);
tampilNilai.putExtras(bundle);
startActivity(showScore);
}
});
}
private void quiz() {
question_numb = question_numb + 1;
if (question_numb < 10) {
Qnum.setText(" " + Integer.toString(question_numb) + '.');
} else Qnum.setText(Integer.toString(question_numb) + '.');
checkResult();
Qcontent.setText(opsys.getString(0)); //this is to set the question I've taken from db to TextView
a.setText(opsys.getString(1)); //this is to set the option A I've taken from db to radiobutton
b.setText(opsys.getString(2)); //this is to set the option B I've taken from db to radiobutton
c.setText(opsys.getString(3)); //this is to set the option C I've taken from db to radiobutton
d.setText(opsys.getString(4)); //this is to set the option D I've taken from db to radiobutton
//To handle when question number is 10
if (question_numb == 10) {
finish();
}
}
private boolean checkResult() {
// TODO Auto-generated method stub
int random10 = generateRandomNumber(10); //I've a method to generate Random number using Math.random() without repetition
int random18 = generateRandomNumber(18);
String query;
Bundle bundle = getIntent().getExtras();
int chooseContent = bundle.getInt("chooseContent");
if (chooseContent == 0) {
query = "SELECT question, a, b, c, d, answer FROM tbl_Exercise WHERE (id_topic=" + random18 + " and id_question=" + random10 + ")";
} else {
query = "SELECT question, a, b, c, d, answer FROM tbl_Exercise WHERE (id_topic=" + chooseContent + " and id_question=" + random10 + ")";
}
opsys = db.rawQuery(query, null);
if (opsys != null && opsys.moveToFirst()) {
return true;
}
opsys.close();
return false;
}
Sometimes when I touch the Next button, the app crashes (but sometimes it doesn't, and it works like charm). The logcat is as follows:
06-04 19:14:16.884 10062-10062/com.coder.learningmodule E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.coder.learningmodule, PID: 10062
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at com.coder.learningmodule.ExerciseQuestion.quiz(ExerciseQuestion.java:106)
at com.coder.learningmodule.ExerciseQuestion.access$300(ExerciseQuestion.java:19)
at com.coder.learningmodule.ExerciseQuestion$1.onClick(ExerciseQuestion.java:68)
at android.view.View.performClick(View.java:4443)
at android.view.View$PerformClick.run(View.java:18442)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5021)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
at dalvik.system.NativeStart.main(Native Method)
The error refers to Qcontent.setText(opsys.getString(0));
in quiz()
method
After I read some related question in Stackoverflow, I have tried the following code, but it does not solve the problem
opsys = db.rawQuery(query, null);
if (opsys != null && opsys.moveToFirst()) {
return true;
}
opsys.close();
return false;
I would really appreciate help and/or suggestion from anyone to solve this problem. Thank you very much.
Aucun commentaire:
Enregistrer un commentaire