我正在嘗試從here的Android應用程序使用Google Sign-In。 我可以用Google帳戶&成功登錄,並可以獲取所有詳細信息。然而,當過我嘗試註銷失敗與以下錯誤:使用Google Sign-In註銷選項使用java.lang.IllegalStateException崩潰:GoogleApiClient尚未連接
java.lang.IllegalStateException: GoogleApiClient is not connected yet.
我看了很多答案提示創建裏面的onCreate(googleClientApi對象),這就是我所作所爲。已經添加了連接和懸浮回調但連接永遠不會進入暫停模式。
以下是我的代碼片段:
public static void doInit(Context ctx, FragmentActivity fragmentActivity) {
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(
GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(ctx)
.enableAutoManage(fragmentActivity , googleAuth)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.addConnectionCallbacks(googleAuth)
.build();
}
public static Intent doGoogleLogIn() {
return Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
}
public static boolean doGoogleLogOut() {
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
}
});
return true;
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
// An unresolvable error has occurred and Google APIs (including Sign-In) will not
// be available.
Log.d("Signin", "onConnectionFailed:" + connectionResult);
}
@Override
public void onConnected(@Nullable Bundle bundle) {
System.out.println("Connected...");
}
@Override
public void onConnectionSuspended(int i) {
System.out.println("Suspened....");
}
這是值得懷疑的,以我的唯一的事情是,當我登錄並創建googleApiClient對象,它從不同的活動創建的,是我現在用的註銷之一。我不懷疑這是原因,因爲當活動加載時,googleApiClient上的isConnected返回true。但是,當我做一些UI操作(點擊註銷)時,它開始返回false。