我有这个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();
给了我这个例外:
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)
我真的不明白为什么会这样。显然,我正在尝试使用LocationClient
在运行连接方法并等待onConnected
回调之后获取最后一个已知位置,据我所知,onConnected
当我有连接时会调用回调,那么我怎样才能得到"Not connected ..."
异常呢?有人知道吗?
谢谢。