每次我按下对话框的正面按钮我的应用程序崩溃时出现nullpointerexception但我不知道我要改变什么。我认为问题是由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);
}
}
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}
答案 0 :(得分:1)
尝试用getView()
替换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();
答案 1 :(得分: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);
...等