空对象引用上的'android.content.Intent com.google.android.gms.auth.api.signin.GoogleSignInClient.getSignInIntent()'

问题描述 投票:0回答:1

我正在尝试在我的应用中实施Google登录,但我不断收到此错误'android.content.Intent com.google.android.gms.auth.api.signin.GoogleSignInClient.getSignInIntent()' on a null object reference

我按照firebase网站上的教程 - > https://firebase.google.com/docs/auth/android/google-signin

这是我的代码

private void googleSignIn() {

    Intent intent = googleSignInClient.getSignInIntent();
    startActivityForResult(intent, RC_SIGN_IN);
}

@Override
public 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(Objects.requireNonNull(account));
        } catch (ApiException e) {
            Log.w("hhm", "Google signin failed", e);
        }
    }
}
android android-intent google-api google-signin
1个回答
1
投票

你初始化了googleSignInClient对象吗?

看起来你的googleSignInClient是null,需要初始化:

1 - 在登录活动的onCreate方法中,配置Google登录以请求您的应用所需的用户数据...

2 - 然后,同样在登录活动的onCreate方法中,使用您指定的选项创建GoogleSignInClient对象。

// Configure sign-in to request the user's ID, email address, and basic
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestEmail()
        .build();

// Build a GoogleSignInClient with the options specified by gso.
googleSignInClient = GoogleSignIn.getClient(this, gso);

Reference


1
投票

是的,我也有像你这样的问题,这就是我解决的问题

 private Boolean checkgglogin(){
        GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
        if (account != null){
            return true;
        }else
            return false;
    }
//then add checkgglogin to if requestCode
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN && checkgglogin()) {
            // The Task returned from this call is always completed, no need to attach
            // a listener.
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            handleSignInResult(task);
        }
    }

© www.soinside.com 2019 - 2024. All rights reserved.