简体   繁体   中英

java.lang.IllegalStateException: GoogleApiClient must be connected

I'm making this app where I need to verify the user's email with google login.

I am getting the java.lang.IllegalStateException when getting the account name in the onConnected method!

It only happens after choosing the account to use. When it happens, account gets saved but not cleared because of the error so next time it doesn't need to ask which account to use and it works again. Both client.isConnected() and client.isConnecting return false when the error occurs. This doesn't make sense to me because the GoogleApiClient should always be connected in the onConnected method. My code:

@Override
public void onConnected(Bundle bundle){
    //connected to Google plus
    Log.d(TAG, "onConnected");
    mShouldResolve = false;

    //get email
    //error happens during the getAccountName() call
    prefs.edit().putString("email", Plus.AccountApi.getAccountName(googleClient)).apply();

    //user needs to select account next time too: for debug purposes
    Plus.AccountApi.clearDefaultAccount(googleClient);
    //we just needed the email
    googleClient.disconnect();

    view.findViewById(R.id.loginProgressBar).setVisibility(View.INVISIBLE);

    //show "Create new account?"
    chooseRegister();
}//onConnected

I know calling the client.disconnect() during onConnected is untypical but the error happens before it.

EDIT 2: This is in fact in a fragment, and the client init is as follows:

googleClient = new GoogleApiClient.Builder(getActivity())
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API)
            .addScope(new Scope(Scopes.PROFILE))
            .build();

where getActivity() is parent activity and this is the fragment. Does this have an effect? Should I make the parent activity the interface implementer and just redirect the calls to the fragment?

EDIT 3: Tried using the parent activity as the listener to the callbacks, it didn't help either.

EDIT: Somebody will ask for it anyways even though it's pretty useless so: the error stack trace:

java.lang.IllegalStateException: GoogleApiClient must be connected.
com.google.android.gms.common.internal.zzx.zza(Unknown Source)
com.google.android.gms.plus.Plus.zzf(Unknown Source)
com.google.android.gms.internal.zzqe.getAccountName(Unknown Source)
com.myfirm.myproject.LoginFragment.onConnected(LoginFragment.java:148)
com.google.android.gms.common.internal.zzk.zzh(Unknown Source)
com.google.android.gms.internal.zzlg.zznU(Unknown Source)
com.google.android.gms.internal.zzlg.onConnected(Unknown Source)
com.google.android.gms.internal.zzli$2.onConnected(Unknown Source)
com.google.android.gms.common.internal.zzj$zzg.zzpf(Unknown Source)
com.google.android.gms.common.internal.zzj$zza.zzc(Unknown Source)
com.google.android.gms.common.internal.zzj$zza.zzt(Unknown Source)
com.google.android.gms.common.internal.zzj$zzc.zzph(Unknown Source)
com.google.android.gms.common.internal.zzj$zzb.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6117)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

I met that error before, maybe this link will help you:
http://www.androidhive.info/2014/02/android-login-with-google-plus-account-1/
And you must keep Google Client connect when using Google Plus API

Ok I solved it: the fragment is re-created for some reason so the googleClient gets re-created too and the new one isn't connected.

Logically this shouldn't affect the other fragment's client but it's a static variable. (doesn't actually need to be anymore, forgot to change)

Not sure yet but the fragment is probably re-created because the account chooser of the google client is a fragment. So for others with the same problem: multi-tiered fragments are the problem. They're often confusing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM