Android radio group onCheckChangedListener crashing app

前端 未结 3 1852
走了就别回头了
走了就别回头了 2020-12-20 08:20

Any idea why this would crash my app when I select a radio button?

I\'ve imported android.widget.RadioGroup.OnCheckedChangeListener, and I\'ve also trie

相关标签:
3条回答
  • 2020-12-20 09:00

    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.

    0 讨论(0)
  • 2020-12-20 09:07

    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.

    0 讨论(0)
  • 2020-12-20 09:17

    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).

    0 讨论(0)
提交回复
热议问题