无法在Android中使用Firebase Google Auth登录

迪格维杰

我正在尝试在Android中集成由firebase auth提供的google登录。我所做的一切与firebase文档中给出的相同。当用户选择要登录的帐户时,应用停止并在下面给出的log cat窗口中显示错误。

Caused by: java.lang.NullPointerException: Attempt to invoke virtual 
method 'com.google.android.gms.tasks.Task com.google.firebase.auth.FirebaseAuth.signInWithCredential(com.google.firebase.auth.AuthCredential)' on a null object reference
    at com.app.bookbudi.Login.firebaseAuthWithGoogle(Login.java:99)
    at com.app.bookbudi.Login.onActivityResult(Login.java:86)

这是我的代码:

Login.java

public class Login extends AppCompatActivity {

SignInButton signIn;
GoogleSignInClient mGoogleSignInClient;
private static final int RC_SIGN_IN = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

signIn = findViewById(R.id.signIn);

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).
            requestIdToken(getString(R.string.default_web_client_id)).requestEmail().build();

mGoogleSignInClient = GoogleSignIn.getClient(this,gso);

signIn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent signInIntent = mGoogleSignInClient.getSignInIntent();
            startActivityForResult(signInIntent, RC_SIGN_IN);
        }
    });

 }

 @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == RC_SIGN_IN){

        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);

        try{
        GoogleSignInAccount account = task.getResult(ApiException.class);
        firebaseAuthWithGoogle(account);

    }catch(ApiException e){

            Toast.makeText(getApplicationContext(),"Login failed",TastyToast.LENGTH_SHORT).show();
        }
    }
  }

 private void firebaseAuthWithGoogle(GoogleSignInAccount acct){

    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(),null);

    fAuth.signInWithCredential(credential).addOnCompleteListener(this,new OnCompleteListener<AuthResult>() {

        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {

            if(task.isSuccessful()){

                Intent i = new Intent(Login.this,MainActivity.class);
                startActivity(i);
                finish();
            }
            else{

                Toast.makeText(getApplicationContext(),"Unable to login with google.",TastyToast.LENGTH_SHORT).show();

            }
        }
    });


  }

}

请让我知道我在上面的代码中做错了什么。

谢谢

蒂切尔哈兹

您没有初始化Firebase身份验证。

SignInButton signIn;
GoogleSignInClient mGoogleSignInClient;
private static final int RC_SIGN_IN = 1;
//Add Firebase Auth
private FirebaseAuth fAuth;

在此onCreate

fAuth = FirebaseAuth.getInstance(); 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Android中使用Google帐户登录

Google身份工具包,Google OAauth,Firebase Auth和Google+登录之间有什么区别

如何使用django social_auth登录google oauth后打开原始页面

如何解决Google登录集成中的“ com.google.android.gms.auth.api.signin.internal.SignInHubActivity” ActivityNotFoundException问题

使用Google`gms.auth.UserRecoverableAuthException:BadAuthentication`异常登录

Firebase Google登录无法正常运行(Android)

Firebase Auth Ui Google登录“开发人员错误”

升级后,Firebase Auth UI出现Google登录错误

Flutter Firebase Auth / Google_sign_in无法使用statuscode = CANCELED登录

使用django-rest-auth在Android中进行Google登录

如何检查用户是否已经使用Google登录和Firebase Auth登录以检索其数据?

R Googlsheets:无法在googlesheets包中使用`gs_auth()`-使用Google登录暂时禁用的应用程序未验证问题

如何使用Google Auth在Firebase中区分登录和注册用户?

使用Firebase与Google登录

我可以为使用google auth提供程序登录的用户构建文档集合吗?

Firestore规则,request.auth.uid与我从firebase auth google登录获得的uid不同

无法在Android Studio中使用Google登录身份验证

Flutter Firebase Auth无法在Google Play中使用

Facebook和Google+登录名无法在Android中使用已签名的APK

Google auth2 facebook 登录无效

使用 Firebase 的 Google 登录方法无法通过 Android Studio 中的版本控制共享项目

无法在flutter中使用firebase auth登录或注册电子邮件和密码

尝试使用 firebase_auth 登录 Google 时 Android 上的 PlatformException

Firebase Auth 登录区别

如何检测 google auth 登录用户是否是 NewUser 或不使用 Firebase 在 React Native 中

如何使用 Google Auth JWT 授权登录用户?(图片)

Google Auth 自动登录

Nuxt auth 模块无法登录

如何在使用 google firebase auth 登录之前查明用户是否有以前的登录凭据