Firebase发送密码重置电子邮件方法会导致应用崩溃

时间:2017-03-23 09:39:45

标签: android firebase firebase-authentication

我正在尝试忘记密码活动,其中有一个EditText字段供用户输入他们的电子邮件并单击一个按钮,并且将调用sendPasswordResetEmail()方法...所以我的代码就是这个

mAuth = FirebaseAuth.getInstance();
        resetPasswordBtn = (Button)findViewById(R.id.resetPasswordButton);
        resetPasswordFld = (EditText)findViewById(R.id.resetPasswordField);

        final String email = resetPasswordFld.getText().toString().trim();

        resetPasswordBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mAuth.sendPasswordResetEmail(email)
                        .addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                                if (task.isSuccessful()) {
                                    Toast.makeText(ResetPassword.this, "e-mail buat reset password sudah dikirimkan",Toast.LENGTH_LONG).show();
                                    Intent loginIntent = new Intent (ResetPassword.this, LoginActivity.class);
                                    startActivity(loginIntent);
                                }
                            }
                        });
            }
        });

当我尝试它时,应用程序崩溃,我收到了此错误日志

03-23 16:28:59.829 6990-6990E/UncaughtException: java.lang.IllegalArgumentException: Given String is empty or null
                                                                            at com.google.android.gms.common.internal.zzac.zzdv(Unknown Source)
                                                                            at com.google.firebase.auth.FirebaseAuth.sendPasswordResetEmail(Unknown Source)
                                                                            at com.ResetPassword$1.onClick(ResetPassword.java:37)
                                                                            at android.view.View.performClick(View.java:6207)
                                                                            at android.widget.TextView.performClick(TextView.java:11094)
                                                                            at android.view.View$PerformClick.run(View.java:23639)
                                                                            at android.os.Handler.handleCallback(Handler.java:751)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                            at android.os.Looper.loop(Looper.java:154)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6688)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
03-23 16:28:59.833 6990-7024 D/FA: Logging event (FE): _ae, Bundle[{_o=crash, _sc=ResetPassword, _si=-8440426569227389093, timestamp=1490261339829, fatal=1}]
03-23 16:29:00.128 6990-6990 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     Process: , PID: 6990
                                                                     java.lang.IllegalArgumentException: Given String is empty or null
                                                                         at com.google.android.gms.common.internal.zzac.zzdv(Unknown Source)
                                                                         at com.google.firebase.auth.FirebaseAuth.sendPasswordResetEmail(Unknown Source)
                                                                         at.ResetPassword$1.onClick(ResetPassword.java:37)
                                                                         at android.view.View.performClick(View.java:6207)
                                                                         at android.widget.TextView.performClick(TextView.java:11094)
                                                                         at android.view.View$PerformClick.run(View.java:23639)
                                                                         at android.os.Handler.handleCallback(Handler.java:751)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                         at android.os.Looper.loop(Looper.java:154)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:6688)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

我实际上确信我这样做是错误的,因为没有用户登录就确实会返回null吗?我很难找到关于这方面的好文档,而firebase文档中的文档并没有真正帮助。

必须有一种方法可以引用数据库来检查输入的电子邮件地址是否确实存在...但我的规则会阻止没有登录的用户访问它们...所以我真的很困惑如何开始这个......

感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

我认为错误来自这一行:

mAuth.sendPasswordResetEmail(email)

由于您将email标记为final,因此在onClick发生时,它不会更新。并且email值始终为空,因为首先resetPasswordFld确实为空。所以你应该这样做:

// I believe resetPasswordFld is accessible from inside OnClickListener, so

resetPasswordBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        // place this code here, rather than make it final and place it outside
        String email = resetPasswordFld.getText().toString().trim();

        mAuth.sendPasswordResetEmail(email) ...
    }
}

希望这有帮助