The application i'm working on, works properly sometimes but some other times it gives me this error
FATAL EXCEPTION: java.lang.RuntimeException: Unable to pause activity {com.example.dell.locationapi/com.example.dell.locationapi.MainActivity}: java.lang.IllegalStateException: GoogleApiClient is not connected yet.
And sometimes this error :
java.lang.IllegalStateException: GoogleApiClient is not connected yet.
even though the onConnected()
function is called.
I moved the calling of buildGoogleApiClient()
to the begining ofonCreate()
here is my code:
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
}
calling the methods in onCreate()
:
loc = new MyLocation(MainActivity.this);
if (loc.checkPlayServices()) {
loc.buildGoogleApiClient();
loc.createLocationRequest();
}
but it keeps giving the same error sometimes, any idea why this happens?!