我正在尝试编写一个表格,并且有一次有 2 个单选按钮询问此人的性别。我想要求至少按下一个按钮,否则他们会收到祝酒词。它们出现在 activity_main 中,如下所示:
<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>
以及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;
}
可能值得注意的是,我尝试只使用一个 & 来执行 if 语句,但单击单选按钮后我的应用程序仍然崩溃。
这是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
谢谢。