原因:java.lang.IllegalStateException:GoogleApiClient 尚未连接
Caused by: java.lang.IllegalStateException: GoogleApiClient is not connected yet
我在尝试为 Google 实施注销时收到此错误消息 为 Android 登录:
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);
}
在我的扩展应用程序 activity 中(用于存储 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[=15=]0(NavigationDrawerFragment.java:31)
at com.me.myapp.NavigationDrawerFragment.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.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
如有任何帮助,我们将不胜感激。谢谢!
您应该放弃线程并创建第二个 GoogleApiClient
。根据此 post () 客户端不是一个沉重的对象,因此不妨避免混乱的设计并使事情变得简单。即使你不走这条路,你也应该去掉 #setClient
和 #getClient
代码,看看在从单个 activity
断开连接时是否会出现相同的错误
我知道它很旧 post 并且已经回答了。
然而,真正的错误原因不是在一个或多个地方创建对象,而是在构建客户端对象时"enableAutoManage"调用。
API 文档 here 建议它将通过调用 activity.
的 onStart 和 onStop 方法自动进行生命周期管理
因此,如果您想在不同的活动中使用同一个对象,那么您应该避免调用 "enableAutoManage" 和调用 apiObject.connect(最好在 activity 的 onStart 中调用)和 apiObject.disconnect() (最好在 activity 的 onStop 中)。
这对我有用,因此分享。
删除这个:
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
要在另一个Activity中制作一个退出按钮,例如:登录在Activity A中,退出在activity B中,则可以第二个使用这个 activity.
首先创建 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);
}
});
您可以查看是否已连接。
if (mGoogleApiClient.isConnected()) {
//your code
}
我在尝试为 Google 实施注销时收到此错误消息 为 Android 登录:
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);
}
在我的扩展应用程序 activity 中(用于存储 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[=15=]0(NavigationDrawerFragment.java:31)
at com.me.myapp.NavigationDrawerFragment.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.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
如有任何帮助,我们将不胜感激。谢谢!
您应该放弃线程并创建第二个 GoogleApiClient
。根据此 post () 客户端不是一个沉重的对象,因此不妨避免混乱的设计并使事情变得简单。即使你不走这条路,你也应该去掉 #setClient
和 #getClient
代码,看看在从单个 activity
我知道它很旧 post 并且已经回答了。
然而,真正的错误原因不是在一个或多个地方创建对象,而是在构建客户端对象时"enableAutoManage"调用。
API 文档 here 建议它将通过调用 activity.
的 onStart 和 onStop 方法自动进行生命周期管理因此,如果您想在不同的活动中使用同一个对象,那么您应该避免调用 "enableAutoManage" 和调用 apiObject.connect(最好在 activity 的 onStart 中调用)和 apiObject.disconnect() (最好在 activity 的 onStop 中)。
这对我有用,因此分享。
删除这个:
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
要在另一个Activity中制作一个退出按钮,例如:登录在Activity A中,退出在activity B中,则可以第二个使用这个 activity.
首先创建 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);
}
});
您可以查看是否已连接。
if (mGoogleApiClient.isConnected()) {
//your code
}