简体   繁体   中英

How to check which radio buttons are checked upon pressing a button

I'm getting this error when I click on the button that executes a method.

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) 

The method is called upon by android:onClick through xml upon clicking the button.

This is the method,

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;
        }
    }

this is the xml entry for the button,

<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" />

I looked at the error and noticed that it says it's caused because radiobutton widget cannot be cast to button widget, is that right? if so how can I implement this. What I'm trying to do is simply check the radio button's current state(checked or not) once the button is clicked. My previous approach, which worked, was this exact method but called from a radiobutton. I had put an android:onClick on the radiobutton's xml file. But this somehow does not work.

Found the solution, apparently I need to use radiogroups instead of directly using radiobuttons, the only change I made is in the code, here it is,

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
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM