DialogFragment:显示第一个对话框,在肯定按钮上,显示第二个对话框,在第二个对话框上,再次显示第一个对话框

时间:2019-08-13 08:47:46

标签: java android

我有一个像这样的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;
    }

}

这就是我想要发生的事情。

  1. 显示第一个对话框,它是身份验证密码框对话框。
  2. 在肯定按钮上单击(“确定按钮”):
  3. 显示另一个对话框,即“错误对话框”。
  4. 在肯定按钮上单击“错误对话框”,返回到第一个对话框。

它返回以下异常: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Dialog.show()' on a null object reference,当我单击第二个对话框的肯定按钮时,该按钮应该返回到第一个主密码对话框。

我在这里做什么错了?

1 个答案:

答案 0 :(得分:0)

所以我通过以下方法解决了这个问题:

  1. 在onCreate方法之外为密码对话框和错误对话框创建空值。
  2. 在onCreate方法中声明密码对话框和错误对话框的值。
  3. 在活动中显示DialogFragment show方法上的第一个对话框(密码对话框)。
  4. 发生错误时,使用errorDialog.show()方法关闭第一个对话框,然后显示第二个对话框(错误对话框)
  5. 在第二个对话框的“确定”按钮上,关闭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;
    }
    
    }