gpt4 book ai didi

android - 带有单选按钮的 NoSuchMethodException

转载 作者:行者123 更新时间:2023-11-29 14:38:57 28 4
gpt4 key购买 nike

我正在尝试编写一个表单,有一次有 2 个单选按钮询问此人的性别。我想要求至少按下一个按钮,否则他们就会 toast 。它们出现在 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

谢谢。

最佳答案

创建一个方法onRadioButtonClicked,因为您已经在xml 中设置了onClick。所以你必须在 Activity 中有一个可以处理点击的方法。

方法

public void onRadioButtonClicked(View v){
// Your code on click
}

关于android - 带有单选按钮的 NoSuchMethodException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18733326/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com