Android Studio: using radioButtons for a quiz
So, I am trying to create an application where you select a response from radioButton
within radioGroup
, and when you click the Submit button, it will change textbox
to say Correct or Invalid answer, depending on the button you selected. I can launch the application and select radioButton
, but when I press the submit button, the application crashes and says "Sorry, MyApp has stopped."
This is the code 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 figure out what's wrong!
EDIT: I posted the solution. Purchase @miselking for one problem.
source to share
Ok, so you need to know that when you use a way android:onClick="checkResult"
to define click events, your method checkResult
must have View
an argument, only then can it respond to the onClick of the Event. So, change your method to something like this:
private void checkResult(View v) {
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");
}
EDIT: What you are getting is a warning, not an error. You are getting this warning because you are not using a parameter v
anywhere in your method. You can ignore it, useful if you have multiple buttons calling the same method, then you need to know which button is actually being called by the method.
Let's say you have two buttons with ids btnId1
and btnId2
. They both have this line of code in their xml: file android:onClick="checkResult"
, so they both call the same method (you can do that). Then when you click either of those two buttons, which one actually called the method? Well, that's why View v
you need it. Then you could see which button was clicked and responded accordingly. Implementation example checkResult
:
public void checkResult(View view)
{
Log.d("TAG_BTN", "Someone called me. But who???");
switch (view.getId())
{
case R.id.btnId1:
Log.d("TAG_BTN", "BtnId1 called me. Do something, please...");
break;
case R.id.btnId2:
Log.d("TAG_BTN", "BtnId2 called me. What next to do...");
break;
}
}
Hope you know why you need the parameter View
.
source to share