Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not connected. Call Connect or wait for onConnected() to be called exception inside onConnected?

I have this onConnected call back implemented for Google LocationClient API:

@Override
public void onConnected(Bundle arg0) {
    if (lc != null) {
        lastKnownLocation = lc.getLastLocation();
        LocationRequest request = new LocationRequest();
        request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        request.setFastestInterval(MIN_TIME_BETWEEN_LOCATION_UPDATES);
        request.setSmallestDisplacement(MIN_DISTANCE_BETWEEN_LOCATION_UPDATES);
        lc.requestLocationUpdates(request, ll);
    }
}

And for some reason, sometimes this line:

lastKnownLocation = lc.getLastLocation();

Gives me this exception:

java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
   at com.google.android.gms.internal.de.bc()
   at com.google.android.gms.internal.ez.a()
   at com.google.android.gms.internal.ez$c.bc()
   at com.google.android.gms.internal.ey.getLastLocation()
   at com.google.android.gms.internal.ez.getLastLocation()
   at com.google.android.gms.location.LocationClient.getLastLocation()
   at com.citylifeapps.cups.helputils.UserLocation.onConnected(UserLocation.java:115)
   at com.google.android.gms.internal.de.aZ()
   at com.google.android.gms.internal.de$f.a()
   at com.google.android.gms.internal.de$f.a()
   at com.google.android.gms.internal.de$b.be()
   at com.google.android.gms.internal.de$a.handleMessage()
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:157)
   at android.app.ActivityThread.main(ActivityThread.java:5356)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
   at dalvik.system.NativeStart.main(NativeStart.java)

I really can't understand why this happens. Clearly I'm trying to get the last known location using the LocationClient after running the connect method and waiting for the onConnected callback and as I understand the onConnected call back is called when I have connection, So how can I get the "Not connected ..." exception? Does some one knows?

Thanks.

like image 702
Emil Adz Avatar asked Oct 20 '22 10:10

Emil Adz


1 Answers

Maybe like me, you did not call connect() at all. Try with the call included in the onStart method:

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}
like image 101
sethi.anuj Avatar answered Nov 03 '22 00:11

sethi.anuj