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

后端 未结 5 745
暖寄归人
暖寄归人 2021-01-18 02:13

I am getting this error message when trying to implement logout for Google Sign-In for Android:

Caused by: java.lang.IllegalStateException: GoogleApiClient i         


        
5条回答
  •  春和景丽
    2021-01-18 02:50

    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.

    First create the OnStart method:

     @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() {
            @Override
            public void onResult(Status status) {
                // ...
                Toast.makeText(getApplicationContext(),"Logged Out",Toast.LENGTH_SHORT).show();
                Intent i=new Intent(getApplicationContext(),MainActivity.class);
                startActivity(i);
            }
        });
    

提交回复
热议问题