Question

My app is map-centric. I call requestLocationUpdates() often. Once in a while, I get this exception when its called. Or in any other place which calls such method. I saw the solutions suggested on SOF, sadly nothing seems to work for me. Even if i call mLocationClient.connect(), there is no guarantee that it connects immediately, correct me if i'm wrong. How do i solve this problem?

case R.id.btnContinue:

gpsLocationDailog.cancel();

        int isGooglePlayServiceAvilable = GooglePlayServicesUtil
                .isGooglePlayServicesAvailable(getApplicationContext());
        if (isGooglePlayServiceAvilable == ConnectionResult.SUCCESS) {
            /** thorws shitty expectiosn **/
            mLocationClient.connect();
            try {
                mLocationClient.requestLocationUpdates(REQUEST, this);
                mCurrentLocation = mLocationClient.getLastLocation();
                fireQueryToGetTheResponse(latitude, longitude);
                rl.setVisibility(View.VISIBLE);
                mDrawerLayout.setVisibility(View.GONE);
                mSlidingUpPanelLayout.setVisibility(View.GONE);
            } catch (IllegalStateException e) {
                mLocationClient.connect();
                Log.e(TAG, " Waiting for onConnect to be called");
            }
        } else {
            GooglePlayServicesUtil.getErrorDialog(
                    isGooglePlayServiceAvilable,
                    SqueakeeMapListViewPager.this, 0).show();
        }

        break;

and this is the exception that is raised:

java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
at com.google.android.gms.internal.de.bc(Unknown Source)
at com.google.android.gms.internal.ez.a(Unknown Source)
at com.google.android.gms.internal.ez$c.bc(Unknown Source)
at com.google.android.gms.internal.ey.requestLocationUpdates(Unknown Source)
at com.google.android.gms.internal.ez.requestLocationUpdates(Unknown Source)
at com.google.android.gms.internal.ez.requestLocationUpdates(Unknown Source)
at com.google.android.gms.location.LocationClient.requestLocationUpdates(Unknown Source)
at org.application.app.squeakee.SqueakeeMapListViewPager.onClick(SqueakeeMapListViewPager.java:1936)
at android.view.View.performClick(View.java:4475)
at android.view.View$PerformClick.run(View.java:18786)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5493)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
at dalvik.system.NativeStart.main(Native Method)
Was it helpful?

Solution

I had a similar error, and basically, what was said on the previous comment will resolve your problem yes. If you did trace your error, it would report here:

mLocationClient.requestLocationUpdates(REQUEST, this);

The reason is that the client isn't connected when you call that API method. And that is why the previous answer works just fine, because basically what he did was to start the location updates on the callback.

When you do this:

mLocationClient.connect();

your client won't immediately connect but the code will kept being run, and when you ask locationUpdates you are not connected yet (it is still working on it, it takes it's time), so it crashes. Also, when you do the .connect() you will basically activate the onConnected() callback, so if you code your locationUpdates request inside it, then you basically will solve most of your errors.

Also, you said that you have a lot of requests and only sometimes this error occurs. For that, you didn't provide enough code, but I think I can assume the error happens because of the way/when you cancel your requests and re-activate them (your methods onStop(), onPause(), onResume() and onStart() ). When you pause your app, you should disconnect the client, but you should make sure that when you resume it, you reconnect it again, so maybe just do the

mLocationClient.connect();

inside the onResume() and that possibly fixes some of your other problems (once again, you didn't provide that code, maybe you are coding it right already, but for some other reader that has this problem, this might be a suggestion to fix it).

It's kinda late answer, but hope this helps the rest of the community.

Best regards.

OTHER TIPS

Try to implement GooglePlayServicesClient.ConnectionCallbacks and @Override onConnected() on your activity and put all the code that needs a connection inside of it.

That worked for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top