gpt4 book ai didi

java - Firebase_Auth_API 在 API 23 上不可用

转载 作者:行者123 更新时间:2023-12-02 12:11:52 24 4
gpt4 key购买 nike

我创建了一个 EditText,用于捕获用户名和密码以便稍后存储在 Firebase 中。在运行 API 26 的 AVD 中,我没有遇到任何问题。但是,我最近创建了一个运行 API 23 的 AVD,它在我创建的“loggedInAuth”变量上返回 null。根据我的理解和研究,这是 Firebase 的一个错误。我不确定是否可以在 API <= 23 上运行此代码,或者它是否仅适用于模拟器。感谢任何帮助:

    public void signup() {
Log.d(TAG, "Signup");

if (!validate()) {
onSignupFailed();
return;
}


final ProgressDialog progressDialog = new ProgressDialog(SignupActivity.this,
R.style.AppTheme_Dark_Dialog);
progressDialog.setIndeterminate(true);
progressDialog.setMessage("Creating Account...");
progressDialog.show();

mNewFirebaseUserEmail = _emailText.getText().toString();
mNewFirebaseUserPassword = _passwordText.getText().toString();

new Handler().postDelayed(
new Runnable() {
public void run() {
// On complete call either onSignupSuccess or onSignupFailed
// depending on success
onSignupSuccess();
// onSignupFailed();
progressDialog.dismiss();
}
}, 3000);
onSignupSuccess();
}

onSignupSuccess()

public void onSignupSuccess() {
signupButton.setEnabled(true);
//not sure why this line is here
setResult(RESULT_OK, null);
//this line below actually creates the user in Firebase; does not write or save to any locations
mAuth.createUserWithEmailAndPassword(mNewFirebaseUserEmail, mNewFirebaseUserPassword)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithEmail:onComplete:" + task.isSuccessful());
FirebaseAuth loggedInAuth = FirebaseAuth.getInstance();
NULL BEING THROWN HERE
String ID = loggedInAuth.getCurrentUser().getUid();
//TODO: Update handling of display name
String displayName = mAuth.getCurrentUser().getUid();
HashMap<String, Object> id = new HashMap<>();
id.put("user_id", ID);
id.put("display_name", displayName);
mUserRef.child(ID).updateChildren(id);

// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithEmail:failed", task.getException());
Toast.makeText(SignupActivity.this, "Authentication Failed with Email",
Toast.LENGTH_SHORT).show();
}
}
});
}

错误

 09-30 10:06:25.105 2456-2456/com.troychuinard.fanpolls E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.troychuinard.fanpolls, PID: 2456
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
at com.troychuinard.fanpolls.SignupActivity$5.onComplete(SignupActivity.java:280)
at com.google.android.gms.tasks.zzf.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

最佳答案

尝试一下并检查用户 ID 是否已记录

mAuth.createUserWithEmailAndPassword(mNewFirebaseUserEmail, mNewFirebaseUserPassword)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "createUserWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
Log.d(TAG, "user:"+user.getUid());
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "createUserWithEmail:failure", task.getException());
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}

}
});

关于java - Firebase_Auth_API 在 API 23 上不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46480765/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com