So I'm trying to make an app where you select an answer from a radioButton within a radioGroup, and when you hit the Submit button it will change the textbox to say "Correct" or "Wrong answer", depending on which button was selected. I'm able to run the app, and select the radioButton, but when I click submit, the app crashes and says "Unfortunately, MyApp has stopped".
This is the code that I have:
XML
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/question1"
android:id="@+id/q1radiobox">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/q1a1"
android:id="@+id/q1a1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/q1a2"
android:id="@+id/q1a2"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/q1a3"
android:id="@+id/q1a3"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/q1a4"
android:id="@+id/q1a4"/>
</RadioGroup>
<Button
android:onClick="checkResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/q1radiobox"
android:text="Submit"/>
Java
private void checkResult() {
RadioButton rb;
rb = (RadioButton) findViewById(R.id.q1a3);
if (rb.isChecked()) {
((TextView) findViewById(R.id.answer1)).setText("@string/correct");
}
else {
((TextView) findViewById(R.id.answer1)).setText("@string/incorrect");
}
}
Any help would be greatly appreciated; I can't work out what's wrong!
Aucun commentaire:
Enregistrer un commentaire