Android 用 Google サインインのログアウトを実装しようとすると、次のエラー メッセージが表示されます。
Caused by: java.lang.IllegalStateException: GoogleApiClient is not connected yet.
クラッシュは、signOut() メソッドを呼び出す DrawerActivity.java (以下) で発生します。
私は他の投稿の解決策を見て、それらを無駄にしようとしました:
java.lang.IllegalStateException: GoogleApiClient はまだ接続されていません
GoogleApiClient はまだ接続されていません例外 Fatal Exception: java.lang.IllegalStateException GoogleApiClient はまだ接続されていません
MainActivity.java:
protected void onCreate(Bundle savedInstanceState) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
//... other code for google sign in not shown
}
protected void onStart() {
mGoogleApiClient.connect();
}
private void handleSignInResult(GoogleSignInResult result) {
if (result.isSuccess()) {
App.getInstance().setClient(mGoogleApiClient);
//start DrawerActivity
}
}
DrawerActivity.java (サインアウトを実行したい場所)
private void googleSignOut(){
mGoogleApiClient = App.getInstance().getClient();
Auth.GoogleSignInApi.signOut(mGoogleApiClient);
}
アプリケーションを拡張するアプリ内アクティビティ (GoogleApiClient の保存に使用)
public class App extends Application {
private GoogleApiClient mGoogleApiClient;
private static App mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized App getInstance() {
return mInstance;
}
public void setClient(GoogleApiClient client){
mGoogleApiClient = client;
}
public GoogleApiClient getClient(){
return mGoogleApiClient;
}
}
スタックトレース:
21:33.314 25375-25375/com.me.myapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.me.myapp, PID: 25375
java.lang.IllegalStateException: GoogleApiClient is not connected yet.
at com.google.android.gms.internal.zzmf.zzb(Unknown Source)
at com.google.android.gms.internal.zzmi.zzb(Unknown Source)
at com.google.android.gms.internal.zzmg.zzb(Unknown Source)
at com.google.android.gms.auth.api.signin.internal.zzc.signOut(Unknown Source)
at com.me.myapp.DrawerActivity.googleSignOut(DrawerActivity.java:526)
at com.me.myapp.DrawerActivity.onNavigationDrawerItemSelected(DrawerActivity.java:512)
at com.me.myapp.NavigationDrawerFragment.selectItem(NavigationDrawerFragment.java:201)
at com.me.myapp.NavigationDrawerFragment.access$000(NavigationDrawerFragment.java:31)
at com.me.myapp.NavigationDrawerFragment$1.onItemClick(NavigationDrawerFragment.java:98)
at android.widget.AdapterView.performItemClick(AdapterView.java:310)
at android.widget.AbsListView.performItemClick(AbsListView.java:1145)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3042)
at android.widget.AbsListView$3.run(AbsListView.java:3879)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteI
どんな助けでも大歓迎です。ありがとう!