RadioGroup onCheckedChangeListener 出错

Error on RadioGroup onCheckedChangeListener

我正在创建一个 custom dialog fragment,里面有一个 radiogroup。我在行

中收到错误
rdoGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()

正如我在研究中看到的那样,RadioGroup.OnCheckedChangeListener() 应该只是 OnCheckedChangeListener(),但 android studio 检测到它是一个错误,并告诉我修复方法是将它变成 RadioGroup.OnCheckedChangeListener()

这里是错误

FATAL EXCEPTION: main Process: com.integritynet.lukslofts.services, PID: 10341 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.integritynet.lukslofts.services/com.integritynet.lukslofts.services.main2}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) at android.app.ActivityThread.access0(ActivityThread.java:166) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5584) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.integritynet.lukslofts.services.fragment.layoutChooserFragment.onCreateView(layoutChooserFragment.java:39) at android.app.Fragment.performCreateView(Fragment.java:1700) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062) at android.app.BackStackRecord.run(BackStackRecord.java:684) at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447) at android.app.Activity.performStart(Activity.java:5456) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2402) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)  at android.app.ActivityThread.access0(ActivityThread.java:166)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:136)  at android.app.ActivityThread.main(ActivityThread.java:5584)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)  at dalvik.system.NativeStart.main(Native Method)

这是我的代码:

public class layoutChooserFragment extends DialogFragment {
    Context ctx;
    String rdo;
    public layoutChooserFragment(){

    }
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
        View vi = inflater.inflate(R.layout.layout_picker, container);
        getDialog().setTitle(R.string.txtLayoutChooser);

        final CheckBox cboRemember = (CheckBox)vi.findViewById(R.id.cboRemember);
        RadioGroup rdoGrp = (RadioGroup)vi.findViewById(R.id.rdoGrpPckr);
        final RadioButton rdoGrid = (RadioButton)vi.findViewById(R.id.rdoGrid);
        final RadioButton rdoTab   = (RadioButton)vi.findViewById(R.id.rdoTab);
        rdoGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (rdoTab.isChecked()) {
                    if (cboRemember.isChecked()) {
                        rdo = "tab";
                        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(ctx);
                        SharedPreferences.Editor editor = settings.edit();
                        editor.putString("chosenLayout", rdo);
                        editor.commit();
                        Log.d("tag", "chosenLayout: " + rdo);
                    }
                    Intent i = new Intent(ctx, MainActivity.class);
                    startActivity(i);
                } else if (rdoGrid.isChecked()) {
                    if (cboRemember.isChecked()) {
                        Intent i = new Intent(ctx, main2.class);
                        startActivity(i);
                        rdo = "grid";
                        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(ctx);
                        SharedPreferences.Editor editor = settings.edit();
                        editor.putString("chosenLayout", rdo);
                        editor.commit();
                        Log.d("tag", "chosenLayout: " + rdo);
                    }
                    Intent i = new Intent(ctx, servicesGridStyle.class);
                    startActivity(i);
                }
            }
        });
        //dlg.show();
        return vi;

    }
}

布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp">

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

    <RadioButton
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:button="@null"
        android:drawableRight="@android:drawable/btn_radio"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/txtTab"
        android:id="@+id/rdoTab"
        android:textSize="29sp" />

    <RadioButton
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:button="@null"
        android:drawableRight="@android:drawable/btn_radio"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/txtGrid"
        android:id="@+id/rdoGrid"
        android:textSize="29sp" />
</RadioGroup>

<CheckBox
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/txtRememberChoice"
    android:id="@+id/cboRemember"
    android:textSize="20sp" />
</LinearLayout>

问题的原因是您的布局中没有 R.id.rdoGrpPckr。也许您有另一个 RadioGroup,但 ID 略有不同。检查您的布局 xml 并更正以解决您的问题。

另外请使用标准 Java class naming conventions 作为您的 class 名称 LayoutChooserFragment。

你必须使用

rdoGrp.setOnCheckedChangeListener(new OnCheckedChangeListener() {}

而不是

rdoGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {}

布局如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp">

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

        <RadioButton
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:button="@null"
            android:drawableRight="@android:drawable/btn_radio"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/txtTab"
            android:id="@+id/rdoTab"
            android:textSize="29sp" />

        <RadioButton
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:button="@null"
            android:drawableRight="@android:drawable/btn_radio"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/txtGrid"
            android:id="@+id/rdoGrid"
            android:textSize="29sp" />
    </RadioGroup>

    <CheckBox
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/txtRememberChoice"
        android:id="@+id/cboRemember"
        android:textSize="20sp" />
</LinearLayout>

我假设你正在覆盖

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

你的

里面
rdoGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {}

对吗?

我正在检查我从 onCreate 调用自定义函数 setListeners(); 并设置所有侦听器的应用程序。 您是在 onCreate 中调用此函数吗?您的单选按钮是否全局初始化?