Question

everytime i press the positive button of the dialog my app crashes with an nullpointerexception but i have no idea what i have to change. i think the problem is caused by the part with getText() because it works when i delete it here:

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)getView().findViewById(R.id.editFach);
                                            fach = eF.getText().toString();
                                            EditText eR = (EditText)getView().findViewById(R.id.editRaum);
                                            raum = eR.getText().toString();
                                            EditText eL = (EditText)getView().findViewById(R.id.editLehrer);
                                            lehrer = eL.getText().toString();

                                            save(fach, raum, lehrer, index);
                                        }
                                    })
                                    .setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int id) {
                                            newLessonDialog.this.getDialog().cancel();
                                        }
                                    });
                            return builder.create();
                        }
                    }

and

public static void save(String fach, String raum, String lehrer, int index)
    {
        BufferedWriter out = null;
        try
        {
            out = new BufferedWriter(new FileWriter("/sdcard/" + index + "fach.txt"));
            out.write(fach);
            out = new BufferedWriter(new FileWriter("/sdcard/" + index + "raum.txt"));
            out.write(raum);
            out = new BufferedWriter(new FileWriter("/sdcard/" + index + "lehrer.txt"));
            out.write(lehrer);
            out.close();
        }
        catch (Exception e)
        {
            Toast.makeText(getAppContext(), "Fehler beim Speichern!", Toast.LENGTH_SHORT);
        }
    }

logcat:

08-13 19:14:07.609  23114-23114/de.nathan.android.droidschool W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41760700)
08-13 19:14:07.619  23114-23114/de.nathan.android.droidschool E/AndroidRuntime: FATAL EXCEPTION: main
        java.lang.NullPointerException
        at de.nathan.android.droidschool.MainActivity$fragmentTab1$1$1newLessonDialog$2.onClick(MainActivity.java:267)
        at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        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-13 19:14:07.639     438-6851/? W/ActivityManager: Process de.nathan.android.droidschool has crashed too many times: killing!
08-13 19:14:07.639     438-6851/? W/ActivityManager: Force finishing activity de.nathan.android.droidschool/.MainActivity
08-13 19:14:07.659     438-6851/? I/ActivityManager: Killing proc 23114:de.nathan.android.droidschool/u0a10018: crash
08-13 19:14:07.679      438-551/? W/InputDispatcher: channel '428f0a50 de.nathan.android.droidschool/de.nathan.android.droidschool.MainActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
08-13 19:14:07.679      438-551/? E/InputDispatcher: channel '428f0a50 de.nathan.android.droidschool/de.nathan.android.droidschool.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
08-13 19:14:07.679      438-551/? W/InputDispatcher: channel '41f4abe8 de.nathan.android.droidschool/de.nathan.android.droidschool.MainActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
08-13 19:14:07.679      438-551/? E/InputDispatcher: channel '41f4abe8 de.nathan.android.droidschool/de.nathan.android.droidschool.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
08-13 19:14:07.679      438-448/? W/InputDispatcher: Attempted to unregister already unregistered input channel '41f4abe8 de.nathan.android.droidschool/de.nathan.android.droidschool.MainActivity (server)'
08-13 19:14:07.679      438-449/? W/InputDispatcher: Attempted to unregister already unregistered input channel '428f0a50 de.nathan.android.droidschool/de.nathan.android.droidschool.MainActivity (server)'
08-13 19:14:07.679      438-448/? I/WindowState: WIN DEATH: Window{41f4abe8 u0 de.nathan.android.droidschool/de.nathan.android.droidschool.MainActivity}
08-13 19:14:07.679      438-449/? I/WindowState: WIN DEATH: Window{428f0a50 u0 de.nathan.android.droidschool/de.nathan.android.droidschool.MainActivity}
Was it helpful?

Solution

Try replacing your getView()s with getDialog().

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

OTHER TIPS

You may as well keep a reference to the original view; it'll make things a lot easier. Try:

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        final View view = inflater.inflate(R.layout.new_lesson_dialog, null);
        builder.setView(view)
        .setPositiveButton("Speichern", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                EditText eF = (EditText)view.findViewById(R.id.editFach);

...etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top