简体   繁体   English

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

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

I am getting this error message when trying to implement logout for Google Sign-In for Android:尝试为 Android 的 Google Sign-In 实现注销时,我收到此错误消息:

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

The crash occurs in DrawerActivity.java (below), where I call the signOut() method.崩溃发生在 DrawerActivity.java(如下)中,我在其中调用了 signOut() 方法。

I've looked at the solutions in other posts and have tried them to no avail:我查看了其他帖子中的解决方案,并尝试过无济于事:

java.lang.IllegalStateException: GoogleApiClient is not connected yet java.lang.IllegalStateException: GoogleApiClient 尚未连接

GoogleApiClient is not connected yet exception Fatal Exception: java.lang.IllegalStateException GoogleApiClient is not connected yet GoogleApiClient 尚未连接异常致命异常:java.lang.IllegalStateException GoogleApiClient 尚未连接

MainActivity.java:主活动.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)在 DrawerActivity.java (我想在那里执行注销)

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

In my App activity that extends Application (used to store the GoogleApiClient)在我扩展应用程序的应用程序活动中(用于存储 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!谢谢!

You should ditch the threading and just create a second GoogleApiClient .您应该GoogleApiClient线程并创建第二个GoogleApiClient According to this post ( https://stackoverflow.com/a/25190497/608347 ) the client isn't a heavy object so might as well avoid the confusing design and make things simple.根据这篇文章( https://stackoverflow.com/a/25190497/608347 ),客户端不是重物,所以不妨避免混乱的设计并使事情变得简单。 Even if you dont go down this path you should strip out that #setClient and #getClient code and see if you get the same error when disconnecting from a single activity即使你不走这条路,你也应该#setClient#getClient代码,看看在从单个活动断开连接时是否遇到相同的错误

I know its quite old post and already answered.我知道它很旧的帖子并且已经回答了。

However, the actual cause of the error is not object creation at single or multiple places but "enableAutoManage" invocation at the time of Building Client object.但是,错误的实际原因不是在单个或多个位置创建对象,而是在构建客户端对象时调用“enableAutoManage”。

The API doc here suggests that it would automatically do the life cycle management by calling methods on onStart & onStop methods of the activity. 此处的 API 文档建议它通过调用活动的 onStart 和 onStop 方法上的方法来自动进行生命周期管理。

Therefore, if you want to use the same object across different activities then you should avoid calling "enableAutoManage" and invoke apiObject.connect(preferably in onStart of activity) and apiObject.disconnect() (preferably in onStop of activity).因此,如果您想在不同的活动中使用相同的对象,那么您应该避免调用“enableAutoManage”并调用 apiObject.connect(最好在活动的 onStart 中)和 apiObject.disconnect()(最好在活动的 onStop 中)。

This worked for me, therefore sharing.这对我有用,因此分享。

To make a button Sign Out in another Activity, for example: the login is in the Activity A and the sign out is in the activity B, then you can use this for the second activity.要在另一个Activity中制作一个退出按钮,例如:登录在Activity A中,退出在Activity B中,那么您可以将其用于第二个Activity。

First create the OnStart method:首先创建 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();
}

After in your button collocate this:在你的按钮之后搭配这个:

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

删除这个:

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

you can check whether is connected or not.您可以检查是否已连接。

 if (mGoogleApiClient.isConnected()) {

          //your code
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IllegalStateException:尚未连接GoogleApiClient - java.lang.IllegalStateException: GoogleApiClient is not connected yet java.lang.IllegalStateException:GoogleApiClient尚未连接 - java.lang.IllegalStateException: GoogleApiClient is not connected yet 致命异常:java.lang.IllegalStateException尚未连接GoogleApiClient - Fatal Exception: java.lang.IllegalStateException GoogleApiClient is not connected yet java.lang.IllegalStateException:必须连接GoogleApiClient - java.lang.IllegalStateException: GoogleApiClient must be connected 引起:java.lang.IllegalStateException - Caused by: java.lang.IllegalStateException Android:Google Play游戏服务连接错误(java.lang.IllegalStateException:必须连接GoogleApiClient。) - Android: Google play games services connection error ( java.lang.IllegalStateException: GoogleApiClient must be connected.) 引起:java.lang.IllegalStateException:包未安装? - Caused by: java.lang.IllegalStateException: package not installed? Android:java.lang.IllegalStateException:已经连接 - Android: java.lang.IllegalStateException: Already connected 异常:java.lang.IllegalStateException:已连接 - Exception : java.lang.IllegalStateException: Already connected java.lang.IllegalStateException:已在setDoOutput处连接 - java.lang.IllegalStateException: Already connected at setDoOutput
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM