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 tried
new RadioGroup.OnCheckedChangeListener()
which was the solution to a similar post.
The code is (logcat added):
RadioGroup ringtone_radio_group = (RadioGroup)findViewById(R.id.ringtone_radio_group);
ringtone_radio_group.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
Toast.makeText(getApplicationContext(), "checked id = " + checkedId, Toast.LENGTH_SHORT).show();
}
});
The layout is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="left">
<TextView android:id="@+id/ringtone_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="30sp"
android:text="Hello!"/>
<RadioGroup
android:id="@+id/ringtone_radio_group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton android:id="@+id/ringtone_button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ringtone1"
android:onClick="onRadioButtonClicked"/>
<Button
android:id="@+id/play_button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/ringtone_button_1"
android:text="Play" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton android:id="@+id/ringtone_button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ringtone2"
android:onClick="onRadioButtonClicked"/>
<Button android:id="@+id/play_button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton android:id="@+id/ringtone_button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ringtone3"
android:onClick="onRadioButtonClicked"/>
<Button android:id="@+id/play_button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play" />
</LinearLayout>
</RadioGroup>
</LinearLayout>
The logcat:
05-31 11:09:36.053: E/AndroidRuntime(26969): java.lang.IllegalStateException: Could not find a method onRadioButtonClicked(View) in the activity class com.example.ringtones.MainActivity for onClick handler on view class android.widget.RadioButton with id 'ringtone_button_1'
05-31 11:09:36.053: E/AndroidRuntime(26969): at android.view.View$1.onClick(View.java:3031)
05-31 11:09:36.053: E/AndroidRuntime(26969): at android.view.View.performClick(View.java:3511)
05-31 11:09:36.053: E/AndroidRuntime(26969): at android.widget.CompoundButton.performClick(CompoundButton.java:100)
05-31 11:09:36.053: E/AndroidRuntime(26969): at android.view.View$PerformClick.run(View.java:14105)
05-31 11:09:36.053: E/AndroidRuntime(26969): at android.os.Handler.handleCallback(Handler.java:605)
05-31 11:09:36.053: E/AndroidRuntime(26969): at android.os.Handler.dispatchMessage(Handler.java:92)
05-31 11:09:36.053: E/AndroidRuntime(26969): at android.os.Looper.loop(Looper.java:137)
05-31 11:09:36.053: E/AndroidRuntime(26969): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-31 11:09:36.053: E/AndroidRuntime(26969): at java.lang.reflect.Method.invokeNative(Native Method)
05-31 11:09:36.053: E/AndroidRuntime(26969): at java.lang.reflect.Method.invoke(Method.java:511)
05-31 11:09:36.053: E/AndroidRuntime(26969): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:812)
05-31 11:09:36.053: E/AndroidRuntime(26969): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:579)
05-31 11:09:36.053: E/AndroidRuntime(26969): at dalvik.system.NativeStart.main(Native Method)
05-31 11:09:36.053: E/AndroidRuntime(26969): Caused by: java.lang.NoSuchMethodException: onRadioButtonClicked [class android.view.View]
05-31 11:09:36.053: E/AndroidRuntime(26969): at java.lang.Class.getConstructorOrMethod(Class.java:460)
05-31 11:09:36.053: E/AndroidRuntime(26969): at java.lang.Class.getMethod(Class.java:915)
05-31 11:09:36.053: E/AndroidRuntime(26969): at android.view.View$1.onClick(View.java:3024)
05-31 11:09:36.053: E/AndroidRuntime(26969): ... 12 more
05-31 11:27:02.010: I/Adreno200-EGLSUB(27793): <ConfigWindowMatch:2081>: Format RGBA_8888.
05-31 11:27:02.020: D/memalloc(27793): /dev/pmem: Mapped buffer base:0x509ca000 size:6103040 offset:5488640 fd:55
05-31 11:27:02.160: D/memalloc(27793): /dev/pmem: Mapped buffer base:0x511ba000 size:614400 offset:0 fd:58
05-31 11:27:05.053: I/Adreno200-EGLSUB(27793): <ConfigWindowMatch:2081>: Format RGBA_8888.
05-31 11:27:05.053: D/memalloc(27793): /dev/pmem: Mapped buffer base:0x51467000 size:3645440 offset:3031040 fd:61
05-31 11:27:05.143: D/memalloc(27793): /dev/pmem: Mapped buffer base:0x51855000 size:4874240 offset:4259840 fd:70
05-31 11:27:05.163: D/memalloc(27793): /dev/pmem: Mapped buffer base:0x51cfb000 size:6758400 offset:6717440 fd:73
05-31 11:27:05.183: D/memalloc(27793): /dev/pmem: Unmapping buffer base:0x509ca000 size:6103040 offset:5488640
05-31 11:27:05.183: D/memalloc(27793): /dev/pmem: Unmapping buffer base:0x511ba000 size:614400 offset:0
05-31 11:27:06.944: D/memalloc(27793): /dev/pmem: Unmapping buffer base:0x51cfb000 size:6758400 offset:6717440
05-31 11:27:07.705: D/memalloc(27793): /dev/pmem: Mapped buffer base:0x508ca000 size:614400 offset:0 fd:52
05-31 11:27:08.005: W/dalvikvm(27793): threadid=1: thread exiting with uncaught exception (group=0x40a641f8)
05-31 11:27:08.005: E/AndroidRuntime(27793): FATAL EXCEPTION: main
05-31 11:27:08.005: E/AndroidRuntime(27793): java.lang.IllegalStateException: Could not find a method onRadioButtonClicked(View) in the activity class com.example.ringtones.MainActivity for onClick handler on view class android.widget.RadioButton with id 'ringtone_button_1'
05-31 11:27:08.005: E/AndroidRuntime(27793): at android.view.View$1.onClick(View.java:3031)
05-31 11:27:08.005: E/AndroidRuntime(27793): at android.view.View.performClick(View.java:3511)
05-31 11:27:08.005: E/AndroidRuntime(27793): at android.widget.CompoundButton.performClick(CompoundButton.java:100)
05-31 11:27:08.005: E/AndroidRuntime(27793): at android.view.View$PerformClick.run(View.java:14105)
05-31 11:27:08.005: E/AndroidRuntime(27793): at android.os.Handler.handleCallback(Handler.java:605)
05-31 11:27:08.005: E/AndroidRuntime(27793): at android.os.Handler.dispatchMessage(Handler.java:92)
05-31 11:27:08.005: E/AndroidRuntime(27793): at android.os.Looper.loop(Looper.java:137)
05-31 11:27:08.005: E/AndroidRuntime(27793): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-31 11:27:08.005: E/AndroidRuntime(27793): at java.lang.reflect.Method.invokeNative(Native Method)
05-31 11:27:08.005: E/AndroidRuntime(27793): at java.lang.reflect.Method.invoke(Method.java:511)
05-31 11:27:08.005: E/AndroidRuntime(27793): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:812)
05-31 11:27:08.005: E/AndroidRuntime(27793): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:579)
05-31 11:27:08.005: E/AndroidRuntime(27793): at dalvik.system.NativeStart.main(Native Method)
05-31 11:27:08.005: E/AndroidRuntime(27793): Caused by: java.lang.NoSuchMethodException: onRadioButtonClicked [class android.view.View]
05-31 11:27:08.005: E/AndroidRuntime(27793): at java.lang.Class.getConstructorOrMethod(Class.java:460)
05-31 11:27:08.005: E/AndroidRuntime(27793): at java.lang.Class.getMethod(Class.java:915)
05-31 11:27:08.005: E/AndroidRuntime(27793): at android.view.View$1.onClick(View.java:3024)
05-31 11:27:08.005: E/AndroidRuntime(27793): ... 12 more
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.
来源:https://stackoverflow.com/questions/23973613/android-radio-group-oncheckchangedlistener-crashing-app