3

I am integrating Google Plus in my android app when I was trying to sign in to Google Plus my app just crashed. It says -

java.lang.IllegalStateException: GoogleApiClient must be connected

Below is the line number which stacktrace points towards,

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 have done all steps for integration, added Google play services to my project and registered my app on Google API's site.

Alex Celeste
  • 12,824
  • 10
  • 46
  • 89
Abid Khan
  • 2,451
  • 4
  • 22
  • 45
  • can u post complete code – KOTIOS Aug 23 '14 at 05:53
  • possible duplicate of [Android: Google play games services connection error ( java.lang.IllegalStateException: GoogleApiClient must be connected.)](http://stackoverflow.com/questions/24474986/android-google-play-games-services-connection-error-java-lang-illegalstateexc) – A Nice Guy Jun 16 '15 at 14:38

2 Answers2

-1

First check if it is connected, then do your stuff.

GoogleApiClient mGoogleApiClient;
if(mGoogleApiClient.isConnected()){
  // good
}else{
  //connect it
  mGoogleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_OPTIONAL);
}
Akshay Mahajan
  • 2,064
  • 1
  • 17
  • 20
  • Please don't add the [same answer](http://stackoverflow.com/a/41914564/5292302) to multiple questions. Answer the best one and flag the rest as duplicates. See [Is it acceptable to add a duplicate answer to several questions?](http://meta.stackexchange.com/questions/104227/is-it-acceptable-to-add-a-duplica‌​te-answer-to-several-questions) – Petter Friberg Jan 28 '17 at 20:38
-4

Duplicate of : Android: Google play games services connection error ( java.lang.IllegalStateException: GoogleApiClient must be connected.)

Anyways, modify 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); 
          }
        }
}
Community
  • 1
  • 1
A Nice Guy
  • 2,676
  • 4
  • 30
  • 54