单击执行方法的按钮时出现此错误。
Process: com.example.myapplication, PID: 7757
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6304)
at android.view.View$PerformClick.run(View.java:24803)
at android.os.Handler.handleCallback(Handler.java:794)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6635)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6304)
at android.view.View$PerformClick.run(View.java:24803)
at android.os.Handler.handleCallback(Handler.java:794)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6635)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatButton cannot be cast to android.widget.RadioButton
at com.example.myapplication.MockTest.submitbtn(MockTest.java:66)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6304)
at android.view.View$PerformClick.run(View.java:24803)
at android.os.Handler.handleCallback(Handler.java:794)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6635)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
该方法由android:onClick在单击按钮时通过xml调用。
这是方法
public void submitbtn(View view)
{
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.q1r1: if (checked)
score++;
break;
case R.id.q2r4: if (checked)
score++;
break;
case R.id.q3r4: if (checked)
score++;
break;
case R.id.q4r2: if (checked)
score++;
break;
}
}
这是按钮的xml条目,
<Button
android:id="@+id/submitbtn"
android:layout_width="100dp"
android:layout_height="65dp"
android:layout_marginStart="150dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="150dp"
android:text="Submit"
android:onClick="submitbtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/q4r" />
我查看了该错误,并注意到该错误是由于无法将单选按钮小部件强制转换为按钮小部件引起的,对吗?如果是这样,我该如何实施呢?我想做的就是单击按钮后简单地检查单选按钮的当前状态(是否选中)。我以前的有效方法是这种确切的方法,但是是通过单选按钮调用的。我在单选按钮的xml文件上放置了一个android:onClick。但这不起作用。
答案 0 :(得分:0)
找到了解决方案,显然我需要使用单选按钮组而不是直接使用单选按钮,我所做的唯一更改是在代码中,这里是
public void submitbtn(View view)
{
RadioGroup q4r = findViewById(R.id.q4r);
//Checking which radio button is checked, if any, and then executing tasks
if(q1r.getCheckedRadioButtonId() == R.id.q1r1)
{
//execute tasks
}
else if(q1r.getCheckedRadioButtonId() == -1)
{
//Nothing
}
else
{
//execute tasks
}
}