Android:对话框中的radiogroup中的Nullpointerexception

时间:2013-08-14 14:20:44

标签: android dialog nullpointerexception radio-button radio-group

我想检查我的对话框中是否检查了我的无线电组的单选按钮。我已经尝试过了(正如你在那里看到的那样),但它在这一行中崩溃了:RadioGroup rGroup = (RadioGroup)getDialog().findViewById(R.id.radioGroup);带有nullpointerexception。

这是我的代码:

class newLessonDialog extends DialogFragment {
                        @Override
                        public Dialog onCreateDialog(Bundle savedInstanceState) {
                            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                            // Get the layout inflater
                            LayoutInflater inflater = getActivity().getLayoutInflater();

                            // Inflate and set the layout for the dialog
                            // Pass null as the parent view because its going in the dialog layout
                            builder.setView(inflater.inflate(R.layout.new_lesson_dialog, null))
                                    // Add action buttons,
                                    .setPositiveButton("Speichern", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int id)
                                        {
                                            EditText eF = (EditText)getDialog().findViewById(R.id.editFach);
                                            fach = eF.getText().toString();
                                            EditText eR = (EditText)getDialog().findViewById(R.id.editRaum);
                                            raum = eR.getText().toString();
                                            EditText eL = (EditText)getDialog().findViewById(R.id.editLehrer);
                                            lehrer = eL.getText().toString();

                                            if (tag != null)
                                            {
                                                save(fach, raum, lehrer, tag, index);
                                            }
                                            else
                                            {
                                                Toast.makeText(getAppContext(), "Zuerst Wochentag auswählen!", Toast.LENGTH_SHORT);
                                            }
                                        }
                                    })
                                    .setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int id) {
                                            newLessonDialog.this.getDialog().cancel();
                                        }
                                    });

                            // This will get the radiogroup
                            RadioGroup rGroup = (RadioGroup)getDialog().findViewById(R.id.radioGroup);
                            // This will get the radiobutton in the radiogroup that is checked
                            RadioButton checkedRadioButton = (RadioButton)rGroup.findViewById(rGroup.getCheckedRadioButtonId());

                            rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
                            {
                                public void onCheckedChanged(RadioGroup rGroup, int checkedId)
                                {
                                    // This will get the radiobutton that has changed in its check state
                                    RadioButton checkedRadioButton = (RadioButton)rGroup.findViewById(checkedId);
                                    // This puts the value (true/false) into the variable
                                    boolean isChecked = checkedRadioButton.isChecked();
                                    // If the radiobutton that has changed in check state is now checked...
                                    if (isChecked)
                                    {
                                        tag = checkedRadioButton.getText().toString();
                                    }
                                }
                            });

                            return builder.create();
                        }


                    }

这是我的logcat:

08-14 16:16:23.749    7618-7618/de.nathan.android.droidschool D/AndroidRuntime: Shutting down VM
08-14 16:16:23.749    7618-7618/de.nathan.android.droidschool W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41760700)
08-14 16:16:23.779    7618-7618/de.nathan.android.droidschool E/AndroidRuntime: FATAL EXCEPTION: main
        java.lang.NullPointerException
        at de.nathan.android.droidschool.MainActivity$fragmentTab1$1$1newLessonDialog.onCreateDialog(MainActivity.java:303)
        at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:295)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
08-14 16:16:23.789      438-665/? W/ActivityManager: Force finishing activity de.nathan.android.droidschool/.MainActivity
08-14 16:16:24.309      438-454/? W/ActivityManager: Activity pause timeout for ActivityRecord{41ef07a8 u0 de.nathan.android.droidschool/.MainActivity}
08-14 16:16:34.899      438-454/? W/ActivityManager: Activity destroy timeout for ActivityRecord{41ef07a8 u0 de.nathan.android.droidschool/.MainActivity}

3 个答案:

答案 0 :(得分:1)

我没有测试代码,但听起来像getDialog因为尚未创建对话框而返回null:

View myView = inflater.inflate(R.layout.new_lesson_dialog);
RadioGroup rGroup = (RadioGroup)myView.findViewById(R.id.radioGroup);
//Do wathever you want with RadioGroup

然后

builder.setView(myView)

注意: 在充气之前请花点时间阅读: http://www.doubleencore.com/2013/05/layout-inflation-as-intended/

答案 1 :(得分:0)

而不是getActivity,请尝试将活动名称添加为:

    AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);

答案 2 :(得分:0)

您正在onCreateDialog()方法中调用getDialog()。因此,尚未创建对话框,应该需要Null指针。

你也需要覆盖onStart并在那里设置你的视图,因为那时对话框已经创建了。

See this answer