我在尝试为Android版Google登录实现注销时收到此错误消息:
Caused by: java.lang.IllegalStateException: GoogleApiClient is not connected yet.
崩溃发生在 DrawerActivity.java(下图),我在其中调用 signOut() 方法。
我查看了其他帖子中的解决方案,并尝试了它们无济于事:
java.lang.IllegalStateException:GoogleApiClient尚未连接
GoogleApi客户端未连接但异常致命异常:java.lang.IllegalStateException GoogleApiClient 尚未连接
主活动.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
}
}
在抽屉活动中.java(我想在其中执行注销)
private void googleSignOut(){
mGoogleApiClient = App.getInstance().getClient();
Auth.GoogleSignInApi.signOut(mGoogleApiClient);
}
在我的应用活动中,扩展了应用程序(用于存储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
任何帮助将不胜感激。谢谢!
你应该放弃线程,只创建一个GoogleApiClient
。根据这篇文章(https://stackoverflow.com/a/25190497/608347),客户不是一个重物,所以不妨避免混乱的设计,让事情变得简单。即使您不走这条路,也应该删除该#setClient
并#getClient
代码,并查看在断开与单个活动的连接时是否遇到相同的错误
我知道它的旧帖子并且已经回答了。
但是,错误的实际原因不是在单个或多个位置创建对象,而是在构建客户端对象时"启用自动管理"调用。
这里的 API 文档建议它通过调用活动的 onStart 和 onStop 方法自动执行生命周期管理。
因此,如果要在不同的活动中使用相同的对象,则应避免调用"enableAutoManage"并调用apiObject.connect(最好在活动的onStart 中)和apiObject.disconnect()(最好在活动的onStop中)。
这对我有用,因此分享。
要使按钮在其他活动中注销,例如:登录位于活动 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);
}
});
删除以下内容:
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
您可以检查是否已连接。
if (mGoogleApiClient.isConnected()) {
//your code
}