GoogleSignInAccount.getIdToken()引发空对象引用错误

时间:2019-07-14 10:17:55

标签: android firebase android-studio firebase-authentication

当我尝试登录google帐户时收到此错误。我已经在互联网上查看了,但是找不到与此错误有关的任何信息。

这是错误:

  

试图调用虚拟方法'java.lang.String   com.google.android.gms.auth.api.signin.GoogleSignInAccount.getIdToken()'   在空对象引用上

这是引发此错误的代码。

    }

    private void Initialize() {
        accountreference = FirebaseDatabase.getInstance().getReference("Account");
        email = findViewById(R.id.loginedittextemail);
        password = findViewById(R.id.loginedittextpassword);
        login = findViewById(R.id.loginbuttonlogin);
        register = findViewById(R.id.loginbuttonregister);

        firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
        if (firebaseUser != null) {
            Intent i = new Intent(LoginActivity.this, HomeActivity.class);
            startActivity(i);
            finish();
        } else {



            login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    signIn();
                }
            });
        }
    }
    public void _ShowDialog() {
        progressDialog = new ProgressDialog(LoginActivity.this);
        progressDialog.setTitle("RandomChat");
        progressDialog.setMessage("Giriş Yapılıyor...");
        progressDialog.setCancelable(false);
        progressDialog.show();

    }

    private void signIn() {
        Intent signInIntent = mGoogleSignInClient.getSignInIntent();
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }
    @Override
    protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(intent);
            try {
                GoogleSignInAccount acct = result.getSignInAccount();
                firebaseAuthWithGoogle(acct);
            }catch (Exception e){
                System.out.println(e.getMessage());
            }
        }
    }

    }
    private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {

    // [START_EXCLUDE silent]
    // [END_EXCLUDE]

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

    mAuth.signInWithCredential(credential)
            .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
                        FirebaseUser user = mAuth.getCurrentUser();
                        Intent i = new Intent(LoginActivity.this,HomeActivity.class);
                        startActivity(i);
                        finish();
                    } else {
                        // If sign in fails, display a message to the user.
                        System.out.println("Giriş başarısız...");

                    }

                    // [START_EXCLUDE]
                    // [END_EXCLUDE]
                }
            });
}

1 个答案:

答案 0 :(得分:0)

似乎您正在尝试获取空getIdToken()对象的GoogleSignInAccount。通过GoogleSignIn.getSignedInAccountFromIntent(data)

的结果可以看到错误代码

或者您在构建Google登录客户端时忘记添加.requestIdToken(...)