Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
772 views
in Technique[技术] by (71.8m points)

How to fix java.lang.IllegalStateException: Could not execute method for android:onClick

I am a newbie trying to create a simple quiz app in Android Studio. Things were progressing reasonably well until I tried to create an onClick event for a radiogroup.

I took a look at this previously answered question: java.lang.IllegalStateException: Could not execute method for android:onClick Android App but the solution did not shed any light on my issue.

Another thing I noticed was a problem with the imported AppCompatViewInflater.java WHICH 'cannot resolve symbol 'R''?

Any advice would much appreciated.

This is the logcat error message that I see:

02-05 09:19:16.989 29473-29473/com.example.hughdavidson_quizapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.hughdavidson_quizapp, PID: 29473
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
        at android.view.View.performClick(View.java:5205)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
        at android.view.View$PerformClick.run(View.java:21164)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
        at android.view.View.performClick(View.java:5205)?
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)?
        at android.view.View$PerformClick.run(View.java:21164)?
        at android.os.Handler.handleCallback(Handler.java:739)?
        at android.os.Handler.dispatchMessage(Handler.java:95)?
        at android.os.Looper.loop(Looper.java:148)?
        at android.app.ActivityThread.main(ActivityThread.java:5417)?
        at java.lang.reflect.Method.invoke(Native Method)?
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)?
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)?
     Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f0c0004


This is the MainActivity.java code

///


public class MainActivity extends AppCompatActivity {
    RadioGroup  radioGroupQ1;
    ImageView q1ImageView;

    TextView textViewQ2;
    ImageView q2ImageView;

    CheckBox checkBoxQ3_i, checkBoxQ3_ii, checkBoxQ3_iii, checkBoxQ3_iv, checkBoxQ3_v;
    ImageView q3_i_ImageView, q3_ii_ImageView, q3_iii_ImageView, q3_iv_ImageView, q3_v_ImageView;

    boolean hasCheckBoxQ3_i = false;
    boolean hasCheckBoxQ3_ii = false;
    boolean hasCheckBoxQ3_iii = false;
    boolean hasCheckBoxQ3_iv = false;
    boolean hasCheckBoxQ3_v = false;

    EditText editTextQ4_i_Answer, editTextQ4_ii_Answer, editTextQ4_iii_Answer, editTextQ4_iv_Answer;
    ImageView q4_i_ImageView, q4_ii_ImageView, q4_iii_ImageView, q4_iv_ImageView;

    int userScore = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Ref for working with radiogroups: https://stackoverflow.com/questions/9748070/radio-group-onclick-event-not-firing-how-do-i-tell-which-is-selected
        radioGroupQ1 = (RadioGroup) findViewById(R.id.q1_radio_buttons);
        radioGroupQ1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch(checkedId){
                    case R.id.q1_radioButton_false:
                        userScore += + 0;
                        q1ImageView.setImageResource(R.mipmap.red_cross);
                        break;
                    case R.id.q1_radioButton_true:
                        userScore += + 1;
                        q1ImageView.setImageResource(R.mipmap.green_tick);
                        break;
                }
            }
        });

        textViewQ2 = (TextView) findViewById(R.id.q2_editText);

        checkBoxQ3_i = (CheckBox) findViewById(R.id.checkBoxQ3_i) ;
        checkBoxQ3_ii = (CheckBox) findViewById(R.id.checkBoxQ3_ii) ;
        checkBoxQ3_iii = (CheckBox) findViewById(R.id.checkBoxQ3_iii) ;
        checkBoxQ3_iv = (CheckBox) findViewById(R.id.checkBoxQ3_iv) ;
        checkBoxQ3_v = (CheckBox) findViewById(R.id.checkBoxQ3_v) ;

        editTextQ4_i_Answer = (EditText) findViewById(R.id.q4_i_editBox);
        editTextQ4_ii_Answer = (EditText) findViewById(R.id.q4_ii_editBox);
        editTextQ4_iii_Answer = (EditText) findViewById(R.id.q4_iii_editBox);
        editTextQ4_iv_Answer = (EditText) findViewById(R.id.q4_iv_editBox);

        q1ImageView = (ImageView) findViewById(R.id.q1_redcross_image_view);
        q2ImageView = (ImageView) findViewById(R.id.q2_redgreencheck_image_view);
        q3_i_ImageView = (ImageView) findViewById(R.id.q3_i_greencheck_image_view);
        q3_ii_ImageView = (ImageView) findViewById(R.id.q3_ii_greencheck_image_view);
        q3_iii_ImageView = (ImageView) findViewById(R.id.q3_iii_redcheck_image_view);
        q3_iv_ImageView = (ImageView) findViewById(R.id.q3_iv_greencheck_image_view);
        q3_v_ImageView = (ImageView) findViewById(R.id.q3_v_greencheck_image_view);
        q4_i_ImageView = (ImageView) findViewById(R.id.q4_i_redgreencheck_image_view);
        q4_ii_ImageView = (ImageView) findViewById(R.id.q4_ii_redgreencheck_image_view);
        q4_iii_ImageView = (ImageView) findViewById(R.id.q4_iii_redgreencheck_image_view);
        q4_iv_ImageView = (ImageView) findViewById(R.id.q4_iv_redgreencheck_image_view);

    }

    //Show users total score
    public void showScore(View view){
        //Check score for question 1

        //Check score for question 2
        String q2Answer = "1000000";
        if (textViewQ2.getText().toString().equals(q2Answer)){
            userScore = userScore + 1;
            q2ImageView.setImageResource(R.mipmap.green_tick);
        }
        else q2ImageView.setImageResource(R.mipmap.red_cross);

        //check score for question 3
        //has Q3-i been ticked
        hasCheckBoxQ3_i = checkBoxQ3_i.isChecked();
        //has Q3-ii been ticked
        hasCheckBoxQ3_ii = checkBoxQ3_ii.isChecked();
        //has Q3-iii been ticked
        hasCheckBoxQ3_iii = checkBoxQ3_iii.isChecked();
        //has Q3-iv been ticked
        hasCheckBoxQ3_iv = checkBoxQ3_iv.isChecked();
        //has Q3-v been ticked
        hasCheckBoxQ3_v = checkBoxQ3_i.isChecked();

        if(hasCheckBoxQ3_i){
            userScore += +1;
            q3_i_ImageView.setImageResource(R.mipmap.green_tick);
        }
        if(hasCheckBoxQ3_ii){
            userScore += +1;
            q3_ii_ImageView.setImageResource(R.mipmap.green_tick);
        }
        if(hasCheckBoxQ3_iii){
            userScore += +1;
            q3_iii_ImageView.setImageResource(R.mipmap.green_tick);
        }
        if(hasCheckBoxQ3_iv){
            userScore += +1;
            q3_iv_ImageView.setImageResource(R.mipmap.green_tick);
        }
        if(hasCheckBoxQ3_v){
            userScore += +0;
            q3_v_ImageView.setImageResource(R.mipmap.red_cross);
        }

        //Check score for Q4
        //Reference: string comparison https://stackoverflow.com/questions/16143562/string-comparison-android/16143606#:~:text=The%20%3D%3D%20operator%20checks%20to,to%20compare%20strings%20for%20equality.
        String Q4_i = "E";
        String Q4_ii = "A";
        String Q4_iii = "C";
        String Q4_iv = "B";

        if(editTextQ4_i_Answer.getText().toString().equals(Q4_i)){
            userScore += +1;
            q4_i_ImageView.setImageResource(R.mipmap.green_tick);
        }
        else q4_i_ImageView.setImageResource(R.mipmap.red_cross);

        if(editTextQ4_ii_Answer.getText().toString().equals(Q4_ii)){
            userScore += +1;
            q4_ii_ImageView.setImageResource(R.mipmap.green_tick);
        }
        else q4_ii_ImageView.setImageResource(R.mipmap.red_cross);

        if(editTextQ4_iii_Answer.getText().toString().equals(Q4_iii)){
            userScore += +1;
            q4_iii_ImageView.setImageResource(R.mipmap.green_tick);
        }
        else q4_iii_ImageView.setImageResource(R.mipmap.red_cross);

        if(editTextQ4_iv_Answer.getText().toString().equals(Q4_iv)){
            userScore += +1;
            q4_iv_ImageView.setImageResource(R.mipmap.green_tick);
        }
        else q4_iv_ImageView.setImageResource(R.mipmap.red_cross);
        
        //  Toast message to user with score
        Context context = getApplicationContext();
        CharSequence text = "You scored: " + userScore + " out of 11";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }

...
}
///

and this is the XML:
///
<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:context=".MainActivity">

        <TextView
            android:id="@+id/q1"
            style="@style/QuestionLabel"
            android:text="@string/q1" />

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"/>

        <TextView
            android:id="@+id/q1_text"
            style="@style/QuestionText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/q1_text"/>

        <RadioGroup
            android:id="@+id/q1_radio_buttons"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
             android:orientation="horizontal"
            app:layout_constraintEnd_toStartOf="@+id/guideline5"
            app:layout_constraintStart_toStartOf="@+id/guideline"
            app:layout_constraintTop_toBottomOf="@+id/q1_text">

            <RadioButton
                android:id="@+id/q1_radioButton_false"

                android:text="@string/q1_radio_button_false"/>

            <RadioButton
                android:id="@+id/q1_radio

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Button onClick and RadioGroup onClick are not the same. Please use this:

       myradioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                
            }
        });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...