Firebase_Auth_API在API 23上不可用

时间:2017-09-29 02:11:52

标签: java android firebase android-edittext firebase-authentication

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

EditText

onSignupSuccess()

    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();
}

BUG

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();
                    }
                }
            });
}

1 个答案:

答案 0 :(得分:1)

尝试此操作并检查用户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();
                    }

                }
            });