无法获得单选按钮状态

时间:2012-02-29 02:18:01

标签: android

我正在使用RadioGroup。我使用以下代码在RadioButton内实现AlertDialog

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
int checkedRadioButton = 0;
    try {
    checkedRadioButton = radioGroup.getCheckedRadioButtonId();
} catch (Exception e) {
    e.printStackTrace();
}
int i =0;
switch (checkedRadioButton) {
case R.id.a2s :
datasource.updateIcon(i,itemid);
break;
case R.id.android: i=1;
datasource.updateIcon(i,itemid);
break;

checkedRadioButton = radioGroup.getCheckedRadioButtonId();在调用时抛出NullPonterException。为什么会这样?

02-29 07:30:30.514: W/System.err(13957): java.lang.NullPointerException
02-29 07:30:30.514: W/System.err(13957):    at com.manager.boot.r1223.OSListActivity$5.onClick(OSListActivity.java:219)
02-29 07:30:30.514: W/System.err(13957):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
02-29 07:30:30.514: W/System.err(13957):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-29 07:30:30.514: W/System.err(13957):    at android.os.Looper.loop(Looper.java:137)
02-29 07:30:30.514: W/System.err(13957):    at android.app.ActivityThread.main(ActivityThread.java:4424)
02-29 07:30:30.514: W/System.err(13957):    at java.lang.reflect.Method.invokeNative(Native Method)
02-29 07:30:30.514: W/System.err(13957):    at java.lang.reflect.Method.invoke(Method.java:511)
02-29 07:30:30.514: W/System.err(13957):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-29 07:30:30.514: W/System.err(13957):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-29 07:30:30.514: W/System.err(13957):    at dalvik.system.NativeStart.main(Native Method)

布局xml

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

    <RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <RadioButton
            android:id="@+id/android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Android" />

        <RadioButton
            android:id="@+id/a2s"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Argen2Stone ROM" />
    </RadioGroup>

</LinearLayout>

2 个答案:

答案 0 :(得分:2)

错误发生在这里:RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup);

您的AlertDialog是否使用了setContentView布局?

答案 1 :(得分:0)

您应该仔细检查您的布局。您正在查看的当前视图中不存在R.id.radiogroup,或者它不属于RadioGroup类型。