如何在对话框中访问EditText?

时间:2019-04-23 14:55:28

标签: android android-alertdialog

我正在尝试在对话框中访问EditText字符串,但是出现空对象引用错误,我无法理解原因:

        txtLostCreds.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Create dialog to set new password
                AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                builder.setTitle(R.string.txtResetPassword);
                builder.setView(R.layout.customresetpassworddialog);
                final AlertDialog alert = builder.create();
                builder.setPositiveButton(R.string.txtResetPassword, new DialogInterface.OnClickListener() {
                    final EditText resetNewPassword = alert.findViewById(R.id.resetNewPassword);
                    final EditText resetNewPasswordConfirm = alert.findViewById(R.id.resetNewPasswordConfirm);
                    @Override
                    public void onClick(final DialogInterface dialog, int which) {
                        assert resetNewPassword != null;
                        assert resetNewPasswordConfirm != null;
                        if (checkPasswords(resetNewPassword, resetNewPasswordConfirm)) {
                           // reset the password
                           RequestParams params = new RequestParams();
                           params.put("uuid", prefs.getString("myUUID", ""));
                           params.put("newpassword", resetNewPassword.getText().toString());
                           AsyncHttpRequest.get("lost_credentials", params, new TextHttpResponseHandler() {
                               @Override
                               public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                                   t.MyToast(getResources().getString(R.string.txtConnError), 5000);
                               }

                               @Override
                               public void onSuccess(int statusCode, Header[] headers, String responseString) {
                                   if (responseString.equals("OK")) {
                                       // All done. Close dialog
                                       alert.cancel();
                                   } else {
                                       t.MyToast(getResources().getString(R.string.txtConnError) + responseString, 5000);
                                   }
                               }
                           });
                       }
                   }
                });
                builder.show();
            }
        });

当我调用函数checkPasswords(resetNewPassword, resetNewPasswordConfirm)时发生错误。我也曾尝试在findViewByID上进行View v,但它给出了相同的错误。

该功能非常简单:

private boolean checkPasswords(EditText resetNewPassword, EditText resetNewPasswordConfirm) {
        if (!resetNewPassword.getText().toString().equals(resetNewPasswordConfirm.getText().toString())) {
            resetNewPassword.setError(getResources().getString(R.string.txtDifferentPasswords));
            resetNewPasswordConfirm.setError(getResources().getString(R.string.txtDifferentPasswords));
            return false;
        } else {
            return true;
        }
    }

1 个答案:

答案 0 :(得分:1)

尝试保留对对话框视图的引用,或者至少保留要用来控制的部分而不是创建对话框的范围。

更好的做法是创建一个DialogFragment,这是对对话框进行更多控制的好方法。当对话框管理许多操作时,这种方法非常有用。

例如:尝试从对话框中获取输入,然后在单击“保存”按钮时将其传递给活动。您可以使用onActivityResult()将数据发送到您的活动中。