0

每次我按下对话框的肯定按钮时,我的应用程序都会因空指针异常而崩溃,但我不知道我必须更改什么。我认为问题是由 getText() 部分引起的,因为当我在这里删除它时它可以工作:

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

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

日志猫:

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}
4

2 回答 2

1

尝试将您getView()的 s替换为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();
于 2013-08-13T17:28:51.820 回答
0

您不妨保留对原始视图的引用;它会让事情变得容易得多。尝试:

    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。

于 2013-08-13T17:32:48.587 回答