Android Firebase密码重置错误

时间:2017-04-14 20:27:42

标签: java android firebase firebase-authentication

每当我尝试将电子邮件输入alertdialog时,我都会收到错误消息。我想让它发送一个恢复密码到输入的电子邮件。这是我的代码和错误信息

代码:

public void PassResetViaEmail(View view)
{
    AlertDialog.Builder alertdialog = new AlertDialog.Builder(Settings.this);
    alertdialog.setTitle("Reset password");
    alertdialog.setMessage("Enter email below");

    EditText input = new EditText(this);
    email = input.getText().toString().trim();
    alertdialog.setView(input);

    alertdialog.setPositiveButton("Send", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            ResetEmailSender();
        }
    });

    alertdialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    alertdialog.show();
}

public void ResetEmailSender()
{
    auth.sendPasswordResetEmail(email).addOnCompleteListener(
        new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    Toast.makeText(Settings.this, "We have sent you instructions to reset your password!", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(Settings.this, "Failed to send reset email!", Toast.LENGTH_SHORT).show();
                }
            }
        });
}

错误:

04-14 02:25:40.842 20031-20031/com.safariagaming.flix E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.safariagaming.flix, PID: 20031
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.tasks.Task com.google.firebase.auth.FirebaseAuth.sendPasswordResetEmail(java.lang.String)' on a null object reference
    at com.safariagaming.flix.Settings.ResetEmailSender(Settings.java:105)
    at com.safariagaming.flix.Settings$2.onClick(Settings.java:91)
    at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:161)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

1 个答案:

答案 0 :(得分:0)

我认为这是问题所在

EditText input = new EditText(this);
email = input.getText().toString().trim();

“电子邮件”仅存在于“ PassResetViaEmail”内部,而不存在于“ ResetEmailSender”内部。试试这个:

alertdialog.setPositiveButton("Send", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            ResetEmailSender(email);
        }
    });

和“ ResetEmailSender”上

public void ResetEmailSender(String email)
{
    auth.sendPasswordResetEmail(email).addOnCompleteListener(
        new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    Toast.makeText(Settings.this, "We have sent you instructions to reset your password!", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(Settings.this, "Failed to send reset email!", Toast.LENGTH_SHORT).show();
                }
            }
        });
}

我假设“ auth”已在其他地方正确实例化。