📅  最后修改于: 2023-12-03 15:25:23.214000             🧑  作者: Mango
在使用 FirebaseGoogleAuth 登录时,有时可能会遇到空对象引用的异常,其中一个可能的原因是调用了虚拟方法 java.lang.String com.google.android.gms.auth.api.signin.GoogleSignInAccount.getIdToken()
导致。
这个方法的作用是获取 GoogleSignInAccount 对象的 ID Token,以便进行后续的验证或者其他操作。但是,如果 GoogleSignInAccount 对象未正确初始化或者未成功登录,就会引发空对象引用异常。下面是一个例子:
public class FirebaseGoogleAuth {
private FirebaseAuth mAuth;
private GoogleSignInClient mGoogleSignInClient;
private static final int RC_SIGN_IN = 9001;
public FirebaseGoogleAuth(Context context) {
// Initialize Firebase Auth and Google Sign-in clients
mAuth = FirebaseAuth.getInstance();
mGoogleSignInClient = GoogleSignIn.getClient(context,
new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(context.getString(R.string.default_web_client_id))
.requestEmail()
.build());
}
public void signIn() {
// Launch the Google Sign In activity
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN); // 注意此处的 startActivityForResult() 方法
// 省略其他代码
}
public class SigninActivity extends AppCompatActivity {
// 定义 FirebaseGoogleAuth 成员变量
private FirebaseGoogleAuth firebaseGoogleAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signin);
// 初始化 FirebaseGoogleAuth
firebaseGoogleAuth = new FirebaseGoogleAuth(this);
// 调用 signIn() 方法开始 Google 登录
firebaseGoogleAuth.signIn();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Pass the activity result back to the Firebase authentication component
if (requestCode == FirebaseGoogleAuth.RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Log.w(TAG, "Google sign in failed", e);
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount account) {
AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
firebaseAuth.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 = firebaseAuth.getCurrentUser();
updateUI(user);
} else {
// If sign in fails, display a message to the user.
Snackbar.make(findViewById(R.id.main_layout), "Authentication Failed.", Snackbar.LENGTH_SHORT).show();
updateUI(null);
}
}
});
}
// 省略其他代码
}
在上述代码中,只需要注意在 FirebaseGoogleAuth
类的 signIn()
方法中,需要使用 startActivityForResult()
方法启动 Google 登录 Activity。这样,当用户成功授权登录后,onActivityResult()
方法会被调用,其中可以通过 GoogleSignInAccount.getIdToken()
方法获取成功登录的用户的 ID Token,用于后续的验证或者其他操作。
如果你遇到了空对象引用的异常,请检查代码是否正确处理了 onActivityResult()
回调对应的 GI、Google 登录操作是否成功,以及是否获得了正确的 GoogleSignInAccount 对象。