任何想法,为什么会崩溃我的应用程序,当我选择一个单选按钮?
我已经导入 android.widget.RadioGroup.OnCheckedChangeListener ,我也试着
新RadioGroup.OnCheckedChangeListener()
这是解决类似的帖子。
在code是(logcat中添加的):
RadioGroup中ringtone_radio_group =(RadioGroup中)findViewById(R.id.ringtone_radio_group);
ringtone_radio_group.setOnCheckedChangeListener(新OnCheckedChangeListener()
{
@覆盖
公共无效onCheckedChanged(RadioGroup中组,诠释checkedId)
{
Toast.makeText(getApplicationContext(),检查ID =+ checkedId,Toast.LENGTH_SHORT).show();
}
});
的布局是:
< XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直
机器人:layout_gravity =左>
< TextView的机器人:ID =@ + ID / ringtone_title
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:重力=center_horizontal
机器人:TEXTSIZE =30sp
机器人:文本=你好!/>
< RadioGroup中
机器人:ID =@ + ID / ringtone_radio_group
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直>
< RelativeLayout的
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向>
<单选机器人:ID =@ + ID / ringtone_button_1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=@字符串/ ringtone1
机器人:的onClick =onRadioButtonClicked/>
<按钮
机器人:ID =@ + ID / play_button_1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentRight =真
机器人:layout_toRightOf =@ ID / ringtone_button_1
机器人:文本=播放/>
< / RelativeLayout的>
<的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向>
<单选机器人:ID =@ + ID / ringtone_button_2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=@字符串/ ringtone2
机器人:的onClick =onRadioButtonClicked/>
<按钮机器人:ID =@ + ID / play_button_2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=播放/>
< / LinearLayout中>
<的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向>
<单选机器人:ID =@ + ID / ringtone_button_3
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=@字符串/ ringtone3
机器人:的onClick =onRadioButtonClicked/>
<按钮机器人:ID =@ + ID / play_button_3
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=播放/>
< / LinearLayout中>
< / RadioGroup中>
< / LinearLayout中>
在logcat的:
11月5日至31日:09:36.053:E / AndroidRuntime(26969):java.lang.IllegalStateException:找不到在活动课com.example onRadioButtonClicked(视图)的方法.ringtones.MainActivity为onclick处理程序上的视图类android.widget.RadioButton ID为'ringtone_button_1
11月5号至31日:09:36.053:E / AndroidRuntime(26969):在android.view.View $ 1.onClick(View.java:3031)
11月5号至31日:09:36.053:E / AndroidRuntime(26969):在android.view.View.performClick(View.java:3511)
11月5号至31日:09:36.053:E / AndroidRuntime(26969):在android.widget.CompoundButton.performClick(CompoundButton.java:100)
11月5号至31日:09:36.053:E / AndroidRuntime(26969):在android.view.View $ PerformClick.run(View.java:14105)
11月5号至31日:09:36.053:E / AndroidRuntime(26969):在android.os.Handler.handleCallback(Handler.java:605)
11月5号至31日:09:36.053:E / AndroidRuntime(26969):在android.os.Handler.dispatchMessage(Handler.java:92)
11月5号至31日:09:36.053:E / AndroidRuntime(26969):在android.os.Looper.loop(Looper.java:137)
11月5号至31日:09:36.053:E / AndroidRuntime(26969):在android.app.ActivityThread.main(ActivityThread.java:4424)
11月5号至31日:09:36.053:E / AndroidRuntime(26969):在java.lang.reflect.Method.invokeNative(本机方法)
11月5号至31日:09:36.053:E / AndroidRuntime(26969):在java.lang.reflect.Method.invoke(Method.java:511)
11月5号至31日:09:36.053:E / AndroidRuntime(26969):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:812)
11月5号至31日:09:36.053:E / AndroidRuntime(26969):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:579)
11月5号至31日:09:36.053:E / AndroidRuntime(26969):在dalvik.system.NativeStart.main(本机方法)
11月5号至31日:09:36.053:E / AndroidRuntime(26969):由:java.lang.NoSuchMethodException:onRadioButtonClicked [类android.view.View]
11月5号至31日:09:36.053:E / AndroidRuntime(26969):在java.lang.Class.getConstructorOrMethod(Class.java:460)
11月5号至31日:09:36.053:E / AndroidRuntime(26969):在java.lang.Class.getMethod(Class.java:915)
11月5号至31日:09:36.053:E / AndroidRuntime(26969):在android.view.View $ 1.onClick(View.java:3024)
11月5号至31日:09:36.053:E / AndroidRuntime(26969):12 ...更多
11月5号至31日:27:02.010的:I /的Adreno200-EGLSUB(27793):其中; ConfigWindowMatch:2081计算值:格式RGBA_8888。
11月5号至31日:27:02.020:D / memalloc(27793)是:/ dev / PMEM:映射缓冲碱:0x509ca000大小:6103040偏移:5488640 FD:55
11月5号至31日:27:02.160:D / memalloc(27793)是:/ dev / PMEM:映射缓冲碱:0x511ba000大小:614400偏移:0 FD:58
11月5号至31日:27:05.053的:I /的Adreno200-EGLSUB(27793):其中; ConfigWindowMatch:2081计算值:格式RGBA_8888。
11月5号至31日:27:05.053:D / memalloc(27793)是:/ dev / PMEM:映射缓冲碱:0x51467000大小:3645440偏移:3031040 FD:61
11月5号至31日:27:05.143:D / memalloc(27793)是:/ dev / PMEM:映射缓冲碱:0x51855000大小:4874240偏移:4259840 FD:70
11月5号至31日:27:05.163:D / memalloc(27793)是:/ dev / PMEM:映射缓冲碱:0x51cfb000大小:6758400偏移:6717440 FD:73
11月5号至31日:27:05.183:D / memalloc(27793)是:/ dev / PMEM:取消映射缓冲碱:0x509ca000大小:6103040偏移:5488640
11月5号至31日:27:05.183:D / memalloc(27793)是:/ dev / PMEM:取消映射缓冲碱:0x511ba000大小:614400偏移:0
11月5号至31日:27:06.944:D / memalloc(27793)是:/ dev / PMEM:取消映射缓冲碱:0x51cfb000大小:6758400偏移:6717440
11月5号至31日:27:07.705:D / memalloc(27793)是:/ dev / PMEM:映射缓冲碱:0x508ca000大小:614400偏移:0 FD:52
11月5号至31日:27:08.005:W / dalvikvm(27793):主题ID = 1:螺纹退出与未捕获的异常(组= 0x40a641f8)
11月5号至31日:27:08.005:E / AndroidRuntime(27793):致命异常:主要
11月5号至31日:27:08.005:E / AndroidRuntime(27793):java.lang.IllegalStateException:找不到在活动课com.example.ringtones.MainActivity对视图类的android onclick处理onRadioButtonClicked(视图)的方法。 widget.RadioButton ID为'ringtone_button_1
11月5号至31日:27:08.005:E / AndroidRuntime(27793):在android.view.View $ 1.onClick(View.java:3031)
11月5号至31日:27:08.005:E / AndroidRuntime(27793):在android.view.View.performClick(View.java:3511)
11月5号至31日:27:08.005:E / AndroidRuntime(27793):在android.widget.CompoundButton.performClick(CompoundButton.java:100)
11月5号至31日:27:08.005:E / AndroidRuntime(27793):在android.view.View $ PerformClick.run(View.java:14105)
11月5号至31日:27:08.005:E / AndroidRuntime(27793):在android.os.Handler.handleCallback(Handler.java:605)
11月5号至31日:27:08.005:E / AndroidRuntime(27793):在android.os.Handler.dispatchMessage(Handler.java:92)
11月5号至31日:27:08.005:E / AndroidRuntime(27793):在android.os.Looper.loop(Looper.java:137)
11月5号至31日:27:08.005:E / AndroidRuntime(27793):在android.app.ActivityThread.main(ActivityThread.java:4424)
11月5号至31日:27:08.005:E / AndroidRuntime(27793):在java.lang.reflect.Method.invokeNative(本机方法)
11月5号至31日:27:08.005:E / AndroidRuntime(27793):在java.lang.reflect.Method.invoke(Method.java:511)
11月5号至31日:27:08.005:E / AndroidRuntime(27793):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:812)
11月5号至31日:27:08.005:E / AndroidRuntime(27793):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:579)
11月5号至31日:27:08.005:E / AndroidRuntime(27793):在dalvik.system.NativeStart.main(本机方法)
11月5号至31日:27:08.005:E / AndroidRuntime(27793):由:java.lang.NoSuchMethodException:onRadioButtonClicked [类android.view.View]
11月5号至31日:27:08.005:E / AndroidRuntime(27793):在java.lang.Class.getConstructorOrMethod(Class.java:460)
11月5号至31日:27:08.005:E / AndroidRuntime(27793):在java.lang.Class.getMethod(Class.java:915)
11月5号至31日:27:08.005:E / AndroidRuntime(27793):在android.view.View $ 1.onClick(View.java:3024)
11月5号至31日:27:08.005:E / AndroidRuntime(27793):12 ...更多
解决方案
你有问题,是因为在XML文件中的onclick,所以只要删除的onclick在你的XML属性和落实onCheckedChangeListener是这样的:
priorRadioGroup =(RadioGroup中)fragmentView.findViewById(R.id.priorityRadioGroup);
priorRadioGroup.setOnCheckedChangeListener(新RadioGroup.OnCheckedChangeListener(){
@覆盖
公共无效onCheckedChanged(RadioGroup中组,诠释checkedId){
开关(checkedId){
//你的code来这里
}
}
});
当你设置的onclick在布局XML文件中,这意味着你应该有你的活动类调用的方法,而你没有。 希望它能帮助。
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
解决方案
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.