将RadioGroup的Android ClassCastException转换为RadioButton

时间:2014-06-05 17:21:13

标签: android classcastexception

这个错误突然冒出来了。过去两天一切顺利。那么为什么这是不正确的:

在第45行的Questionnaire1.java片段中为onCreate():

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

q1.xml摘录如下:

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

这是由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

这已经工作了好几天了,现在又打破了?!是什么给了什么?

感谢任何可以提供帮助的人。

0 个答案:

没有答案