Android ClassCastException for RadioGroup into RadioButton

随声附和 提交于 2019-12-02 21:01:14

问题


This error just popped out of nowhere. Everything was working smoothly for the last 2 days. So why is this incorrect:

In Questionnaire1.java snippet at line 45 for onCreate():

r_male = (RadioButton) findViewById(R.id.radio_male);
r_female = (RadioButton) findViewById(R.id.radio_female);

q1.xml snippet is as follows:

<RadioGroup 
    android:id="@+id/radiogroup1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="100"
    android:paddingLeft="10pt"
    android:paddingBottom="10pt"
>
    <RadioButton android:id="@+id/radio_male"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:text="@string/male"
                 android:onClick="onRadioButtonClicked"
                 android:layout_weight="50"
                 style="@style/text"
    />
    <RadioButton android:id="@+id/radio_female"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:text="@string/female"
                 android:onClick="onRadioButtonClicked"
                 android:layout_weight="50"
                 style="@style/text"
    />
</RadioGroup>

Here is the stack trace as provided by logcat:

06-05 13:08:02.273 E/AndroidRuntime(  951): FATAL EXCEPTION: main
06-05 13:08:02.273 E/AndroidRuntime(  951): java.lang.RuntimeException: Unable to start activity ComponentInfo{ca.nlrt.android.framingham.cvd_2009/ca.nlrt.android.framingham.cvd_2009.Questionnaire1}: java.lang.ClassCastException: android.widget.RadioGroup cannot be cast to android.widget.RadioButton
06-05 13:08:02.273 E/AndroidRuntime(  951):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
06-05 13:08:02.273 E/AndroidRuntime(  951):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
06-05 13:08:02.273 E/AndroidRuntime(  951):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-05 13:08:02.273 E/AndroidRuntime(  951):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
06-05 13:08:02.273 E/AndroidRuntime(  951):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-05 13:08:02.273 E/AndroidRuntime(  951):     at android.os.Looper.loop(Looper.java:137)
06-05 13:08:02.273 E/AndroidRuntime(  951):     at android.app.ActivityThread.main(ActivityThread.java:5103)
06-05 13:08:02.273 E/AndroidRuntime(  951):     at java.lang.reflect.Method.invokeNative(Native Method)
06-05 13:08:02.273 E/AndroidRuntime(  951):     at java.lang.reflect.Method.invoke(Method.java:525)
06-05 13:08:02.273 E/AndroidRuntime(  951):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
06-05 13:08:02.273 E/AndroidRuntime(  951):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-05 13:08:02.273 E/AndroidRuntime(  951):     at dalvik.system.NativeStart.main(Native Method)
06-05 13:08:02.273 E/AndroidRuntime(  951): Caused by: java.lang.ClassCastException: android.widget.RadioGroup cannot be cast to android.widget.RadioButton
06-05 13:08:02.273 E/AndroidRuntime(  951):     at ca.nlrt.android.framingham.cvd_2009.Questionnaire1.onCreate(Questionnaire1.java:45)
06-05 13:08:02.273 E/AndroidRuntime(  951):     at android.app.Activity.performCreate(Activity.java:5133)
06-05 13:08:02.273 E/AndroidRuntime(  951):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-05 13:08:02.273 E/AndroidRuntime(  951):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
06-05 13:08:02.273 E/AndroidRuntime(  951):     ... 11 more
06-05 13:08:02.293 W/ActivityManager(  276):   Force finishing activity ca.nlrt.android.framingham.cvd_2009/.Questionnaire1

This was working for days and now it breaks?! What gives?

Thanks for anyone that can help.

来源:https://stackoverflow.com/questions/24066376/android-classcastexception-for-radiogroup-into-radiobutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!