Android radio group onCheckChangedListener crashing app

痞子三分冷 提交于 2019-11-29 12:45:43

Remove the android:onClick="onRadioButtonClick" from each of your RadioButtons in XML. You should be handling the selection of the RadioButtons inside the OnCheckedChangeListener you are setting on the RadioGroup.

If you need to do special processing when a particular RadioButton is selected, you can use a switch statement inside of the onCheckedChanged callback - the checkedId argument is the android:id value of the selected RadioButton (or -1 if the selection is cleared).

the problem you have is because of the onclick in your XML file so just delete the onclick in your XML attributes and implement onCheckedChangeListener like this:

            priorRadioGroup=(RadioGroup)fragmentView.findViewById(R.id.priorityRadioGroup);
        priorRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch(checkedId) {
                    //your code comes here
                }
            }
        });

when you set onclick in Layout XML file it means you should have the method you called in activity class, and you don't. hope it helps.

You can find the cause of the crash in these lines:

java.lang.IllegalStateException: Could not find a method onRadioButtonClicked(View) in the activity class com.example.ringtones.MainActivity for onClick handler

and

Caused by: java.lang.NoSuchMethodException: onRadioButtonClicked [class android.view.View]

The problem is that you don't have a method onRadioButtonClicked in your MainActivity. Just declare it and you'll be fine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!