引起:java.lang.IllegalStateException:GoogleApiClient尚未连接

时间:2016-03-07 20:44:44

标签: java android android-studio google-api-client android-googleapiclient

我在尝试为Android登录Google登录时收到此错误消息:

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

崩溃发生在DrawerActivity.java(下面)中,我在其中调用signOut()方法。

我已经查看过其他帖子中的解决方案并尝试过无效:

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

在DrawerActivity.java中(我要执行注销)

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

在我的App活动中扩展了Application(用于存储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

任何帮助将不胜感激。谢谢!

4 个答案:

答案 0 :(得分:8)

你应该抛弃线程并创建第二个GoogleApiClient。根据这篇文章(https://stackoverflow.com/a/25190497/608347),客户端不是一个沉重的对象,所以不妨避免混淆设计并简化操作。即使您不沿着这条路走下去,也应该删除#setClient#getClient代码,看看在断开单个活动时是否会出现同样的错误

答案 1 :(得分:5)

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

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

API文档here表明它会通过调用onStart&上的方法自动执行生命周期管理。 onStop活动方法。

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

这对我有用,因此分享。

答案 2 :(得分:0)

删除此内容:

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

答案 3 :(得分:0)

您可以检查是否已连接。

 if (mGoogleApiClient.isConnected()) {

          //your code
  }