Tuesday, 10 September 2013

NoSuchMethodException with a Radio Button

NoSuchMethodException with a Radio Button

I'm attempting to write a form, and at one point there are 2 radio buttons
asking for the gender of the person. I want to require that at least one
button is pressed, or they receive a toast. They appear in the
activity_main as such:
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="53dp"
android:layout_weight="1.00"
android:onClick="onRadioButtonClicked"
android:text="@string/female" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1.00"
android:onClick="onRadioButtonClicked"
android:text="@string/male" />
</RadioGroup>
And the relevant code in the MainActivity:
RadioButton rb1, rb2;
rb1 = (RadioButton) findViewById(R.id.radioButton1);
rb2 = (RadioButton) findViewById(R.id.radioButton2);
if(!rb1.isChecked() && !rb2.isChecked()){
Toast.makeText(this, "Please select a gender.",
Toast.LENGTH_SHORT).show();
return;
}
It may be worth noting that I've tried doing the if statement with only
one &, and my app still crashes upon clicking a radio button.
Here is the LogCat:
09-11 01:21:26.599: E/AndroidRuntime(821): FATAL EXCEPTION: main
09-11 01:21:26.599: E/AndroidRuntime(821):
java.lang.IllegalStateException: Could not find a method
onRadioButtonClicked(View) in the activity class
com.example.myapplication.MainActivity for onClick handler on view class
android.widget.RadioButton with id 'radioButton1'
09-11 01:21:26.599: E/AndroidRuntime(821): at
android.view.View$1.onClick(View.java:3620)
09-11 01:21:26.599: E/AndroidRuntime(821): at
android.view.View.performClick(View.java:4240)
09-11 01:21:26.599: E/AndroidRuntime(821): at
android.widget.CompoundButton.performClick(CompoundButton.java:100)
09-11 01:21:26.599: E/AndroidRuntime(821): at
android.view.View$PerformClick.run(View.java:17721)
09-11 01:21:26.599: E/AndroidRuntime(821): at
android.os.Handler.handleCallback(Handler.java:730)
09-11 01:21:26.599: E/AndroidRuntime(821): at
android.os.Handler.dispatchMessage(Handler.java:92)
09-11 01:21:26.599: E/AndroidRuntime(821): at
android.os.Looper.loop(Looper.java:137)
09-11 01:21:26.599: E/AndroidRuntime(821): at
android.app.ActivityThread.main(ActivityThread.java:5103)
09-11 01:21:26.599: E/AndroidRuntime(821): at
java.lang.reflect.Method.invokeNative(Native Method)
09-11 01:21:26.599: E/AndroidRuntime(821): at
java.lang.reflect.Method.invoke(Method.java:525)
09-11 01:21:26.599: E/AndroidRuntime(821): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-11 01:21:26.599: E/AndroidRuntime(821): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-11 01:21:26.599: E/AndroidRuntime(821): at
dalvik.system.NativeStart.main(Native Method)
09-11 01:21:26.599: E/AndroidRuntime(821): Caused by:
java.lang.NoSuchMethodException: onRadioButtonClicked [class
android.view.View]
09-11 01:21:26.599: E/AndroidRuntime(821): at
java.lang.Class.getConstructorOrMethod(Class.java:423)
09-11 01:21:26.599: E/AndroidRuntime(821): at
java.lang.Class.getMethod(Class.java:787)
09-11 01:21:26.599: E/AndroidRuntime(821): at
android.view.View$1.onClick(View.java:3613)
09-11 01:21:26.599: E/AndroidRuntime(821): ... 12 more
Thanks.

No comments:

Post a Comment