Question

I want to check which radio button of my radiogroup is checked in my dialog. I've already tried it (as you can see down there) but it crashes in this line: RadioGroup rGroup = (RadioGroup)getDialog().findViewById(R.id.radioGroup); with an nullpointerexception.

here's my code:

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();
                        }
                        
                        
                    }

and here's my 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}
Was it helpful?

Solution 2

You are calling getDialog() in the method onCreateDialog(). Therefore, the dialog has not been created yet and a Null pointer should be expected.

You need to override onStart as well and set up your views there instead, since the dialog will have been created by then.

See this answer.

OTHER TIPS

I have nothing to test the code, but sounds like the getDialog return null as the dialog is not created yet:

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

and then

builder.setView(myView)

ATTENTION: Please take some time to read this before inflating: http://www.doubleencore.com/2013/05/layout-inflation-as-intended/

Instead of getActivity try just putting the name of your activity like this:

    AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top