Java 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

我有一个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:当我单击第二个对话框的肯定按钮时,尝试在空对象引用上调用虚拟方法“void android.app.Dialog.show()”
    ,该对话框应返回到第一个主密码对话框


    我做错了什么?

    因此我通过以下方式解决了问题:

  • 在onCreate方法之外为密码对话框和错误对话框创建空值
  • 在onCreate方法中声明密码对话框和错误对话框的值
  • 在活动的DialogFragment Show方法上显示第一个对话框(密码对话框)
  • 出现错误时,关闭第一个对话框,并使用errorDialog.show()方法显示第二个对话框(错误对话框)
  • 在第二个对话框OK按钮上,关闭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;
    }
    
    }