LocationClient not connected to onConnected

Someone already had this problem: - I initialize LocationClient, with ConnectionCallbacks and so on ... - Then I will call "connect ()" on it. - In my onConnected method, I call myLocationClient.getLastLocation (), which causes the application to crash on some devices, except:

"Fatal Exception: java.lang.IllegalStateException Not connected. Connect () and wait for onConnected () to be called."

Any idea?

Here is some piece of code:

myLocationClient = new LocationClient(this, new ConnectionCallbacks() {
        @Override
        public void onDisconnected() {
        //Do some stuff here
        }

        @Override
        public void onConnected(Bundle arg0) {
            if(myLocationClient.getLastLocation() != null) {
                //Do some other stuff here
            }
        }
    }, new OnConnectionFailedListener() {
        @Override
        public void onConnectionFailed(ConnectionResult arg0) {
        //Do other stuff here
        }
});

myLocationClient.connect();

The application crashes in the first line of the onConnected method.

For people who need a stack, this is:

java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
at com.google.android.gms.internal.k.B()
at com.google.android.gms.internal.bh.a()
at com.google.android.gms.internal.bh$c.B()
at com.google.android.gms.internal.bg.getLastLocation()
at com.google.android.gms.internal.bh.getLastLocation()
at com.google.android.gms.location.LocationClient.getLastLocation()
at com.myAppPackage.onConnected(AroundMeActivity.java:321)
at com.google.android.gms.internal.k.y()
at com.google.android.gms.internal.k$f.a()
at com.google.android.gms.internal.k$f.a()
at com.google.android.gms.internal.k$b.D()
at com.google.android.gms.internal.k$a.handleMessage()
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Method.java)
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(NativeStart.java)
+4
source share
1 answer

locationClient.getLastLocation() . Fused Location Provider , . , . , getLastLocation() onConnected(), . , .

, (), , getLastLocation(), .
() - com.google.android.gms.location.LocationListener ( ).

public class MyActivity extends Activity implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener {
// . . . . . . . . more stuff here 
LocationRequest locationRequest;
LocationClient locationClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // . . . . other initialization code
    locationClient = new LocationClient(this, this, this);
    locationRequest = new LocationRequest();
    // Use high accuracy
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    // Set the update interval to 5 seconds
    locationRequest.setInterval(UPDATE_INTERVAL);
    // Set the fastest update interval to 1 second
    locationRequest.setFastestInterval(FASTEST_INTERVAL);
}
// . . . . . . . . other methods 
@Override
public void onConnected(Bundle bundle) {
    Location location = locationClient.getLastLocation();
    if (location == null)
        locationClient.requestLocationUpdates(locationRequest, this);
    else
        Toast.makeText(getActivity(), "Location: " + location.getLatitude() + ", " + location.getLongitude(), Toast.LENGTH_SHORT).show();
}
// . . . . . . . . other methods
@Override
public void onLocationChanged(Location location) {
    locationClient.removeLocationUpdates(this);
    // Use the location here!!!
}

, ( onConnected). , ( onLocationChanged() ) , .

UPDATE:

( ). - , onLocationChanged . , , .

, . , , API Googles. API Androids.

onCreate:

LocationManager manager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER) && !manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
    locationEnabled = false;
    Toast.makeText(getActivity(), "Enable location services for accurate data",     Toast.LENGTH_SHORT).show();
}
else locationEnabled = true;

locationEnabled onConnected :

if (location != null) {
Toast.makeText(getActivity(), "Location: " + location.getLatitude() + ", " + location.getLongitude(), Toast.LENGTH_SHORT).show();
}
else if (location == null && locationEnabled) {
    locationClient.requestLocationUpdates(locationRequest, this);
}

.

+7

Source: https://habr.com/ru/post/1536129/


All Articles