当我选择一个单选按钮时,知道为什么会崩溃我的应用程序?
我已导入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
答案 0 :(得分:2)
从XML中的每个RadioButtons中删除android:onClick="onRadioButtonClick"
。您应该在RadioGroup上设置的OnCheckedChangeListener
内处理RadioButtons的选择。
如果在选择特定RadioButton时需要进行特殊处理,可以在switch
回调中使用onCheckedChanged
语句 - checkedId
参数为{{1}所选RadioButton的值(如果清除选择,则为-1)。
答案 1 :(得分:2)
你遇到的问题是你的XML文件中有onclick所以只需删除XML属性中的onclick并实现onCheckedChangeListener,如下所示:
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
}
}
});
当您在布局XML文件中设置onclick时,这意味着您应该拥有在活动类中调用的方法,而不是。 希望它有所帮助。
答案 2 :(得分:1)
您可以在以下行中找到崩溃的原因:
java.lang.IllegalStateException: Could not find a method onRadioButtonClicked(View) in the activity class com.example.ringtones.MainActivity for onClick handler
和
Caused by: java.lang.NoSuchMethodException: onRadioButtonClicked [class android.view.View]
问题是您的onRadioButtonClicked
中没有方法MainActivity
。只要声明它就可以了。