编程语言
首页 > 编程语言> > Android广播组onCheckChangedListener崩溃的应用程序

Android广播组onCheckChangedListener崩溃的应用程序

作者:互联网

我知道为什么当我选择一个单选按钮时这会导致我的应用程序崩溃?

我已经导入了android.widget.RadioGroup.OnCheckedChangeListener,我也试过了

new RadioGroup.OnCheckedChangeListener()

这是类似帖子的解决方案.

代码是(添加了logcat):

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

布局是:

<?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> 

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

解决方法:

从XML中的每个RadioButtons中删除android:onClick =“onRadioButtonClick”.您应该处理在RadioGroup上设置的OnCheckedChangeListener中的RadioButtons的选择.

如果在选择特定的RadioButton时需要进行特殊处理,可以在onCheckedChanged回调中使用switch语句 – checkedId参数是所选RadioButton的android:id值(如果清除选择,则为-1).

标签:android,android-radiogroup,oncheckedchanged
来源: https://codeday.me/bug/20191002/1841433.html