引起:java.lang.IllegalStateException:GoogleApiClient 尚未连接 [英] Caused by: java.lang.IllegalStateException: GoogleApiClient is not connected yet
问题描述
我在尝试为 Android 版 Google Sign-In 实现注销时收到此错误消息:
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.
崩溃发生在 DrawerActivity.java(如下)中,我在其中调用了 signOut() 方法.
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 尚未连接一个>
GoogleApiClient 尚未连接异常致命异常:java.lang.IllegalStateException GoogleApiClient 尚未连接
MainActivity.java:
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 中(我想在其中执行注销)
In DrawerActivity.java (where I want to perform the sign out)
private void googleSignOut(){
mGoogleApiClient = App.getInstance().getClient();
Auth.GoogleSignInApi.signOut(mGoogleApiClient);
}
在扩展应用程序的我的应用活动中(用于存储 GoogleApiClient)
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;
}
}
堆栈跟踪:
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!
推荐答案
你应该放弃线程,创建第二个 GoogleApiClient
.根据这篇文章(https://stackoverflow.com/a/25190497/608347),客户端并不重object 所以不妨避免混乱的设计并使事情变得简单.即使你不走这条路,你也应该去掉 #setClient
和 #getClient
代码,看看在与单个活动断开连接时是否会出现同样的错误
You should ditch the threading and just create a second 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. 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
这篇关于引起:java.lang.IllegalStateException:GoogleApiClient 尚未连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!