我有一个像这样的DialogFragment类:
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: Attempt to invoke virtual method 'void android.app.Dialog.show()' on a null object reference
,当我单击第二个对话框的肯定按钮时,该按钮应该返回到第一个主密码对话框。
我在这里做什么错了?
答案 0 :(得分:0)
所以我通过以下方法解决了这个问题:
在第二个对话框的“确定”按钮上,关闭errorDialog并使用ad.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;
}
}