我为onConnected实现了这个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);
    }
}

由于某些原因,有时会出现以下行:
lastKnownLocation = lc.getLastLocation();

给我这个异常(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)

我真的不明白为什么会这样。显然,在运行connect方法并等待LocationClient回调后,我试图使用onConnected获取最后一个已知位置,并且据我所知,当我建立连接时会调用onConnected回调,那么如何获得"Not connected ..."异常?
有人知道吗?

谢谢。

最佳答案

也许像我一样,您根本没有调用connect()。尝试使用onStart方法中包含的调用:

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

10-08 17:22