Android: java.lang.IllegalStateException: GoogleApiClient must be connected

I am integrating Google Plus into my android app, when I tried to log into Google Plus my app just crashed. It says -

java.lang.IllegalStateException: GoogleApiClient must be connected

      

Following is the number of lines that the stack points to,

if (Plus.AccountApi.getAccountName(mGoogleApiClient) != null)

@Override
    public void onConnected(Bundle connectionHint) {
        String personName = "Unknown";
        if (Plus.AccountApi.getAccountName(mGoogleApiClient) != null) {
            personName = Plus.AccountApi.getAccountName(mGoogleApiClient); 
        }
}

      

I took all the steps to integrate, added google services to my project and registered my app with the google API site.

+3


source to share


2 answers


First check if it's connected and then do your stuff.



GoogleApiClient mGoogleApiClient;
if(mGoogleApiClient.isConnected()){
  // good
}else{
  //connect it
  mGoogleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_OPTIONAL);
}

      

0


source


Duplicate: Android: Error connecting to google play players (java.lang.IllegalStateException: GoogleApiClient must be connected.)

Anyway, change your code to:



@Override
    public void onConnected(Bundle connectionHint) {
        String personName = "Unknown";
        if (isSignedIn()) {
          if (Plus.AccountApi.getAccountName(mGoogleApiClient) != null) {
            personName = Plus.AccountApi.getAccountName(mGoogleApiClient); 
          }
        }
}

      

-4


source







All Articles