8

I am getting this error message when trying to implement logout for Google Sign-In for Android:

Caused by: java.lang.IllegalStateException: GoogleApiClient is not connected yet.

The crash occurs in DrawerActivity.java (below), where I call the signOut() method.

I've looked at the solutions in other posts and have tried them to no avail:

java.lang.IllegalStateException: GoogleApiClient is not connected yet

GoogleApiClient is not connected yet exception Fatal Exception: java.lang.IllegalStateException GoogleApiClient is not connected yet

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
    }
}

In DrawerActivity.java (where I want to perform the sign out)

private void googleSignOut(){
    mGoogleApiClient = App.getInstance().getClient();
    Auth.GoogleSignInApi.signOut(mGoogleApiClient);
}

In my App activity that extends Application (used to store the 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;
    }
}

StackTrace:

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

Any help would be appreciated. Thanks!

4

5 回答 5

8

您应该放弃线程并创建第二个GoogleApiClient. 根据这篇文章(https://stackoverflow.com/a/25190497/608347),客户端不是一个沉重的对象,所以不妨避免令人困惑的设计并使事情变得简单。即使您不走这条路,您也应该去掉它#setClient#getClient编写代码,看看在与单个活动断开连接时是否遇到相同的错误

于 2016-03-08T04:39:58.663 回答
6

我知道它很老的帖子并且已经回答了。

但是,错误的实际原因不是在单个或多个位置创建对象,而是在构建客户端对象时调用“enableAutoManage”。

此处的 API 文档建议它将通过调用活动的 onStart 和 onStop 方法上的方法来自动进行生命周期管理。

因此,如果您想在不同的活动中使用相同的对象,那么您应该避免调用“enableAutoManage”并调用 apiObject.connect(最好在活动的 onStart 中)和 apiObject.disconnect()(最好在活动的 onStop 中)。

这对我有用,因此分享。

于 2016-10-02T12:44:38.900 回答
2

让一个按钮在另一个活动中退出,例如:登录在活动A中,退出在活动B中,那么您可以将其用于第二个活动。

首先创建 OnStart 方法:

 @Override
protected void onStart() {
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();
    mGoogleApiClient.connect();
    super.onStart();
}

在你的按钮中搭配这个之后:

Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
    new ResultCallback<Status>() {
        @Override
        public void onResult(Status status) {
            // ...
            Toast.makeText(getApplicationContext(),"Logged Out",Toast.LENGTH_SHORT).show();
            Intent i=new Intent(getApplicationContext(),MainActivity.class);
            startActivity(i);
        }
    });
于 2018-02-27T03:20:58.390 回答
0

您可以检查是否已连接。

 if (mGoogleApiClient.isConnected()) {

          //your code
  }
于 2020-10-14T12:28:17.287 回答
0

删除这个:

.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)

于 2017-10-04T06:39:09.343 回答