The application I'm working on sometimes works fine, but sometimes it gives me this error
FATAL EXCEPTION: java.lang.RuntimeException: cannot pause {com.example.dell.locationapi / com.example.dell.locationapi.MainActivity}: java.lang.IllegalStateException: GoogleApiClient is not yet connected.
And sometimes this error:
java.lang.IllegalStateException: GoogleApiClient is not yet connected.
even if the onConnected() function is onConnected() . I translated the call to buildGoogleApiClient() to the beginning of onCreate() , here is my code:
protected synchronized void buildGoogleApiClient() { mGoogleApiClient = new GoogleApiClient.Builder(context) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API).build(); }
calling methods in onCreate() :
loc = new MyLocation(MainActivity.this); if (loc.checkPlayServices()) { loc.buildGoogleApiClient(); loc.createLocationRequest(); }
but sometimes he continues to give the same error, any idea why this is happening ?!
source share