Java DialogFragment:显示第一个对话框,在正面按钮上显示第二个对话框,在第二个对话框上再次显示第一个对话框
我有一个DialogFragment类,如下所示:Java DialogFragment:显示第一个对话框,在正面按钮上显示第二个对话框,在第二个对话框上再次显示第一个对话框,java,android,Java,Android,我有一个DialogFragment类,如下所示: public static class InputPasswordDialog extends DialogFragment { public Dialog onCreateDialog(Bundle savedInstanceState) { super.onCreate(savedInstanceState); AlertDialog.Builder mBuilder = new AlertDia
public static class InputPasswordDialog extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AlertDialog.Builder mBuilder = new AlertDialog.Builder(getActivity());
mBuilder.setTitle("Authentication Required");
mBuilder.setIcon(R.drawable.fingerprint);
View mView = getActivity().getLayoutInflater().inflate(R.layout.management_password_dialog, null);
mBuilder.setView(mView);
mBuilder.setPositiveButton("LOG-IN", new DialogInterface.OnClickListener() {
// LOGIN BUTTON HERE
public void onClick(DialogInterface dialog, int id) {
// I WANT TO SHOW ANOTHER DIALOG (ERROR DIALOG)
new AlertDialog.Builder(getActivity())
.setTitle("Password Error")
.setMessage("Password is incorrect. Please try again!")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// I WANT TO GO BACK TO THE PREVIOUS MAIN DIALOG
getDialog().show();
}
}).show();
}
});
mBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog ad = mBuilder.create();
return ad;
}
}
这就是我想要发生的
java.lang.NullPointerException:当我单击第二个对话框的肯定按钮时,尝试在空对象引用上调用虚拟方法“void android.app.Dialog.show()”
,该对话框应返回到第一个主密码对话框
我做错了什么?因此我通过以下方式解决了问题:
显示第一个对话框->关闭它并显示第二个->关闭第二个对话框并再次显示第一个对话框
public static class InputPasswordDialog extends DialogFragment {
public Dialog ad = null;
public Dialog errorDialog = null;
public Dialog onCreateDialog(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
setCancelable(false);
errorDialog = new AlertDialog.Builder(getActivity())
.setTitle("Password Error")
.setMessage("Password is incorrect. Please try again!")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// I WANT TO GO BACK TO THE PREVIOUS MAIN DIALOG
dialog.dismiss();
ad.show();
}
}).create();
errorDialog.setCancelable(false);
AlertDialog.Builder mBuilder = new AlertDialog.Builder(getActivity());
mBuilder.setTitle("Authentication Required");
mBuilder.setIcon(R.drawable.fingerprint);
View mView = getActivity().getLayoutInflater().inflate(R.layout.management_password_dialog, null);
mBuilder.setView(mView);
mBuilder.setPositiveButton("LOG-IN", new DialogInterface.OnClickListener() {
// LOGIN BUTTON HERE
public void onClick(DialogInterface dialog, int id) {
// I WANT TO SHOW ANOTHER DIALOG (ERROR DIALOG)
dialog.dismiss();
// SHOW ERROR DIALOG OR SUCCESS LOGIN
String password = "password";
if (password.equals(password))
((Management)getActivity()).showManagement();
else
errorDialog.show();
}
});
mBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
getActivity().finish();
}
});
ad = mBuilder.create();
return ad;
}
}