0

我有一个对话框视图,其中有一个 RadioGroup,我从中选择了一个选中的 RadioButton...
但它显示以下错误:

01-28 14:24:02.833: E/AndroidRuntime(4186): FATAL EXCEPTION: main
01-28 14:24:02.833: E/AndroidRuntime(4186): java.lang.NullPointerException
01-28 14:24:02.833: E/AndroidRuntime(4186):     at com.james.restme.MainActivity.onOptionsItemSelected(MainActivity.java:43)
01-28 14:24:02.833: E/AndroidRuntime(4186):     at android.app.Activity.onMenuItemSelected(Activity.java:2534)
01-28 14:24:02.833: E/AndroidRuntime(4186):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
01-28 14:24:02.833: E/AndroidRuntime(4186):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
01-28 14:24:02.833: E/AndroidRuntime(4186):     at com.android.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:156)
01-28 14:24:02.833: E/AndroidRuntime(4186):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
01-28 14:24:02.833: E/AndroidRuntime(4186):     at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
01-28 14:24:02.833: E/AndroidRuntime(4186):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2855)
01-28 14:24:02.833: E/AndroidRuntime(4186):     at android.widget.AbsListView$1.run(AbsListView.java:3529)
01-28 14:24:02.833: E/AndroidRuntime(4186):     at android.os.Handler.handleCallback(Handler.java:615)
01-28 14:24:02.833: E/AndroidRuntime(4186):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-28 14:24:02.833: E/AndroidRuntime(4186):     at android.os.Looper.loop(Looper.java:137)
01-28 14:24:02.833: E/AndroidRuntime(4186):     at java.lang.reflect.Method.invokeNative(Native Method)
01-28 14:24:02.833: E/AndroidRuntime(4186):     at java.lang.reflect.Method.invoke(Method.java:511)

这是类文件...

public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub

    switch (item.getItemId()) {
    case R.id.menu_info:

        break;
    case R.id.menu_config:
        Dialog adc = new Dialog(this);
        adc.setContentView(R.layout.config_dialog);
        RadioGroup rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
        Button btn = (Button) findViewById(R.id.button1);
        rg1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

            }

        });
        adc.setTitle("Custom Dialog");
        adc.show();
        break;
    case R.id.menu_demo:
        break;
    case R.id.menu_act:
        break;

    }

    return super.onOptionsItemSelected(item);
}

这是xml文件...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="set up Account" />

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Check account" />

    <RadioButton
        android:id="@+id/radio2"
        android:layout_width="wrap_content"
        android:checked="true"
        android:layout_height="wrap_content"
        android:text="Change config password" />

    <RadioButton
        android:id="@+id/radio3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Download new skin" />

    <RadioButton
        android:id="@+id/radio4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Download new que" />

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

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="click" />

4

1 回答 1

4

RadioGroup 和按钮在对话框的布局部分.. 尝试如下

 Dialog adc = new Dialog(this);
        adc.setContentView(R.layout.config_dialog);
        RadioGroup rg1 = (RadioGroup) adc.findViewById(R.id.radioGroup1);//check here
        Button btn = (Button) adc.findViewById(R.id.button1);//check here
        rg1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

            }

        });
        adc.setTitle("Custom Dialog");
        adc.show();
于 2014-01-28T15:02:50.047 回答