1

Have created a Google Account login/logout method. Main idea is that when user clicks on

"Sign in with Google", it will navigate the user to the User Profile page and when user decides to logou, the Google logout method will be called and re-direct user back to the HomePage. However, the issue is that when user clicks on the logout button, the following error occurs, what has happened? Please help

I have attached the following code and error log

error log:

01-12 11:38:29.492: E/AndroidRuntime(20881): FATAL EXCEPTION: main
01-12 11:38:29.492: E/AndroidRuntime(20881): java.lang.IllegalStateException: GoogleApiClient must be connected.
01-12 11:38:29.492: E/AndroidRuntime(20881):    at com.google.android.gms.common.internal.n.a(Unknown Source)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at com.google.android.gms.internal.no.a(Unknown Source)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at com.google.android.gms.internal.no.clearDefaultAccount(Unknown Source)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at com.dapoaugury.channelappdemo.ChannelAppMainActivity$DrawerItemClickListener.onItemClick(ChannelAppMainActivity.java:409)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at android.widget.AbsListView.performItemClick(AbsListView.java:1107)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2756)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at android.widget.AbsListView$1.run(AbsListView.java:3430)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at android.os.Handler.handleCallback(Handler.java:725)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at android.os.Looper.loop(Looper.java:137)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at android.app.ActivityThread.main(ActivityThread.java:5039)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at java.lang.reflect.Method.invokeNative(Native Method)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at java.lang.reflect.Method.invoke(Method.java:511)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at dalvik.system.NativeStart.main(Native Method)

Code:

//Logout:
case 2: //DAPO:DEV02-20141231: alternation of login/logout options, login to change to logout when user is login and vice versa

            if (isLogin.equals("Login")){
                //If tab is login, user has not logged in, will navigate user to the login page and allow user to do a Google Login
                Intent intent = new Intent(getApplicationContext(),
                        ChannelAppLoginInfoMainActivity.class);
                startActivity(intent);
            }if (isLogin.equals("Logout")){
                //DAPO:DEV02:20150107:if tab is logout, will navigate user back to home page after user has logged out of Google account.

                Toast.makeText(getApplicationContext(), "Logging out of ChannelApp!", Toast.LENGTH_LONG).show();

                Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
                mGoogleApiClient.disconnect();
                mGoogleApiClient.connect();

                Intent intent= new Intent(getApplicationContext(),
                        ChannelAppMainActivity.class);
                startActivity(intent);
            }
            break;

EDITED CODE:

//DAPO:DEV02-20150108: Declare Google variable:Google+client
private GoogleApiClient mGoogleApiClient;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
//DAPO:DEV02-20150107: Initialize GoogleApiClient variable
    mGoogleApiClient= new GoogleApiClient.Builder(this).addApi(Plus.API).
            addScope(Plus.SCOPE_PLUS_LOGIN).build();
}
//DAPO:DEV02-20150110: Invoking of GoogleApiClient and connecting GoogleApiClient
@Override
protected void onStart() {
    super.onStart();
    // Connect To Drive and Google+
    mGoogleApiClient.connect();
  }
@Override
protected void onStop(){
    super.onStop();
    // Disconnect from Drive and Google+
    mGoogleApiClient.disconnect();
}

protected void onConnected(Bundle ConnectionHint){
    //All Clients are connected
    Intent intent = new Intent(getApplicationContext(),
            ChannelAppAbstractGetNameTask.class);
    startActivity(intent);
}
//DAPO:DEV02-20150110: End of Edited Version of Invoking of GoogleApiClient and connecting GoogleApiClient
androidnoob
  • 79
  • 1
  • 1
  • 9
  • Which one is line# 377 in `ChannelAppMainActivity.java`? – Rohit5k2 Jan 09 '15 at 02:29
  • @Rohit5k2 is this line "mPlusClient.clearDefaultAccount();" – androidnoob Jan 09 '15 at 02:35
  • ohh...`mPlusClient` is null. Please post code of `ChannelAppMainActivity` activity – Rohit5k2 Jan 09 '15 at 02:36
  • @Rohit5k2, what code of ChannelAppMainActivity activity?the above code is part of ChannelAppMainActivity. I have initialized mPLusCLient – androidnoob Jan 09 '15 at 02:38
  • Without looking at the code I wont be able to tell you why its null that why I need to the complete code of that activity. you plus client object is getting null somehow. – Rohit5k2 Jan 09 '15 at 02:39
  • @Rohit5k2 are you able to help? – androidnoob Jan 10 '15 at 03:37
  • Try replacing `PlusClient mPlusClient= new PlusClient.Builder(context,this, mPlusClient.clearDefaultAccount(ChannelAppAbstractGetNameTask.class)).build();` with `mPlusClient= new PlusClient.Builder(context,this, mPlusClient.clearDefaultAccount(ChannelAppAbstractGetNameTask.class)).build();` in `ChannelAppMainActivity` – Rohit5k2 Jan 10 '15 at 03:44
  • @Rohit5k2 Why am I still having syntax error with clearDefaultAccount, it is stating that mPlusClient= new PlusClient.Builder(context,this, mPlusClient.clearDefaultAccount(ChannelAppAbstractGetNameTask.class)).build(); – androidnoob Jan 11 '15 at 04:59
  • @Rohit5k2, i have edited the code, I was reading on the Google development guide and it was saying that Google plusclient is deprecated and is advisable to use GoogleApiclient, hence the above change, however, the error log asked for me to connect the api when the onConnect method for the api is already called – androidnoob Jan 12 '15 at 06:43
  • This problem is solved here http://stackoverflow.com/questions/25458900/android-java-lang-illegalstateexception-googleapiclient-must-be-connected – Rohit5k2 Jan 12 '15 at 14:54
  • @Rohit5k2 Thanks, I have managed to solve my own issue, I knew what happened.Thanks for your help again – androidnoob Jan 13 '15 at 02:34

2 Answers2

2
else{ 
            mPlusClient.clearDefaultAccount();
            mPlusClient.disconnect();
            mPlusClient.connect();
            Intent intent= new Intent(getApplicationContext(),
                    ChannelAppMainActivity.class); 
            startActivity(intent);
        }   

chnge it

else{ 
            mPlusClient.clearDefaultAccount();
            mPlusClient.disconnect();
            mPlusClient.connect();
            Intent intent = getIntent();
            finish();
            startActivity(intent);
        } 
wadali
  • 2,221
  • 1
  • 20
  • 38
-1

Managed to troubleshoot and found the issue that has been plaguing the project:

Initial issue:

  1. GoogleApiClient kept prompting for a connection
  2. Activity kept navigating user to an empty user profile page with no user Google credentials

Solutions:

  1. declare parameters for GoogleApiClient (code listed below)
  2. removed Plus.AccountApi.clearDefaultAccount(mGoogleApiClient); from the logout method as there is no default account to begin with when user first access the app.

Updated code:

mGoogleApiClient= new GoogleApiClient.Builder(this).addApi(Plus.API).
            addScope(Plus.SCOPE_PLUS_LOGIN).build();

//DAPO:DEV02-20150110: Invoking of GoogleApiClient and connecting GoogleApiClient

@Override
protected void onStart() {
    super.onStart();
    // Connect To Drive and Google+
    mGoogleApiClient.connect();
  }
@Override
protected void onStop(){
    super.onStop();
    // Disconnect from Drive and Google+
    if(mGoogleApiClient.isConnected()){
    mGoogleApiClient.disconnect();
    }
}

protected void onConnected(Bundle ConnectionHint){
    //All Clients are connected
    mSignInClicked=false;
    Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
}

/* A helper method to resolve the current ConnectionResult error. */
private void resolveSignInError() {
  if (mConnectionResult.hasResolution()) {
    try {
      mIntentInProgress = true;
      startIntentSenderForResult(mConnectionResult.getResolution().getIntentSender(),
          RC_SIGN_IN, null, 0, 0, 0);
    } catch (SendIntentException e) {
      // The intent was canceled before it was sent.  Return to the default
      // state and attempt to connect to get an updated ConnectionResult.
      mIntentInProgress = false;
      mGoogleApiClient.connect();
    }
  }
}
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
      if (requestCode == RC_SIGN_IN) {
        if (responseCode != RESULT_OK) {
          mSignInClicked = false;
        }

        mIntentInProgress = false;

        if (!mGoogleApiClient.isConnecting()) {
          mGoogleApiClient.connect();
        }
      }
    }
//DAPO:DEV02-20150110: End of Edited Version of Invoking of GoogleApiClient and connecting GoogleApiClient

//Logout:   
        case 2:
            //DAPO:DEV02-20141231: alternation of login/logout options, login to change to logout when user is login and vice versa

            if (isLogin.equals("Login")){
                //If tab is login, user has not logged in, will navigate user to the login page and allow user to do a Google Login
                Intent intent = new Intent(getApplicationContext(),
                        ChannelAppLoginInfoMainActivity.class);
                startActivity(intent);
            }if (isLogin.equals("Logout")){
                //DAPO:DEV02:20150107:if tab is logout, will navigate user back to home page after user has logged out of Google account.

                Toast.makeText(getApplicationContext(), "Logging out of ChannelApp!", Toast.LENGTH_LONG).show();

                //To disconnect user from GoogleApiClient server, remove all Google login credentials.
                mGoogleApiClient.disconnect();
                mGoogleApiClient.connect();

                //Return user to Main HomePage
                startActivity(new Intent(getApplicationContext(), ChannelAppMainActivity.class));
                // make sure the user can not access the page after he/she is logged out
                // clear the activity stack
                finish();
            }
            break;
            //DAPO:DEV02-20141231: End of Edited Version to implement GoogleApiClient logout method and implementation for login/logout method.
androidnoob
  • 79
  • 1
  • 1
  • 9