我在以下代码中添加了一个使用地理围栏的 Android 应用程序:
public void register(ArrayList<Geofence> geofences){
if(geofences == null || geofences.size() == 0) return;
this.geofences = geofences;
locationClient = new LocationClient(context, this, onConnectionFailedListener);
locationClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
locationClient.addGeofences(geofences, GeofenceBroadcastReceiver.getPendingIntent(context), this);
}
@Override
public void onDisconnected() {
this.locationClient = null;
}
@Override
public void onAddGeofencesResult(int statusCode, String[] strings) {
if(statusCode != LocationStatusCodes.SUCCESS){
Log.e(getClass().getName(), "onAddGeofencesResult: " + statusCode);
return;
}
listener.addRegistered(strings);
locationClient.disconnect();
}
每隔一段时间我都会收到以下异常(Crashlytics 链接:http ://crashes.to/s/b63ac8d5f19 ):
java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
at com.google.android.gms.internal.ff.bT()
at com.google.android.gms.internal.hc.addGeofences()
at com.google.android.gms.location.LocationClient.addGeofences()
at nl.auxilium.autoreset.GeofenceManager$AddGeofenceManager.onConnected(GeofenceManager.java:140)
at com.google.android.gms.internal.ff$c.onConnected()
at com.google.android.gms.internal.fg.b()
at com.google.android.gms.internal.fg.bV()
at com.google.android.gms.internal.ff$h.b()
at com.google.android.gms.internal.ff$h.a()
at com.google.android.gms.internal.ff$b.eN()
at com.google.android.gms.internal.ff$a.handleMessage()
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:5751)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
at dalvik.system.NativeStart.main(NativeStart.java)
给定异常, locationClient 未连接,但代码是来自 onConnected 方法的王座。
谁能指出我正确的方向,我似乎无法在我开发的 Galaxy S4 上重现这个错误。