Google Play Services LocationClient的java.util.ConcurrentModificationException

Google Play Services LocationClient的java.util.ConcurrentModificationException,java,android,google-play-services,concurrentmodification,Java,Android,Google Play Services,Concurrentmodification,我正在Android中使用Google Play Services LocationClient。我在获取位置和位置更新方面没有任何问题。但是,当我的应用程序进入后台时,有时应用程序会被android停止,并且会出现以下错误: java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextEntry(HashMap.java:792) at java.util.HashMap$KeyIterator

我正在Android中使用Google Play Services LocationClient。我在获取位置和位置更新方面没有任何问题。但是,当我的应用程序进入后台时,有时应用程序会被android停止,并且会出现以下错误:

java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:792)
at java.util.HashMap$KeyIterator.next(HashMap.java:819)
at com.google.android.gms.internal.l$a$a.onServiceDisconnected(Unknown Source)
at android.app.LoadedApk$ServiceDispatcher.doDeath(LoadedApk.java:1102)
at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1116)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4929)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
at dalvik.system.NativeStart.main(Native Method)
我已多次尝试消除此错误,这就是我的locationClient代码目前的工作方式:

@Override
public void onConnected(Bundle arg0) {
    counter = 0;
    if (getLocationClient().isConnected()) {
        getLocationClient().requestLocationUpdates(getLocationRequest(),
                providerListener);
        ;
        listen();
    } else if (!getLocationClient().isConnecting()){
        getLocationClient().connect();
    }
}

@Override
public void onDisconnected() {
    if(_locationClient!= null && !_locationClient.isConnected() && !_locationClient.isConnecting()) {
        try {
            connectLocationClient();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
getLocationClient和getLocationRequest的方法只是为了确保这些对象不为null:

private LocationClient getLocationClient() {
    if (_locationClient == null) {
        _locationClient = new LocationClient(context, this, this);
    }
    return _locationClient;
}

private LocationRequest getLocationRequest() {
    if (_locRequest == null) {
        _locRequest = new LocationRequest();
        _locRequest.setInterval(fixTime);
        _locRequest
                .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    }
    return _locRequest;
}
你知道会发生什么吗

谢谢

编辑:

正如shr所指出的,在onDisconnected中调用connect()可能是造成这种情况的原因,所以我从中删除了它。在onConnected()中调用requestLocationUpdates()也会引起一些问题,因此:

@Override
public void onConnected(Bundle arg0) {
   requestUpdates();
}
public void requestUpdates(){
   getLocationClient().requestLocationUpdates(getLocationRequest(),this);
}

@Override
public void onDisconnected() {
    //Use a handler instead to reconnect
}

在谷歌开发者控制台崩溃报告中,我在一个大规模部署的应用程序中发现了同样的问题

我怀疑问题是由于我的应用程序试图在
onDisconnected()
方法中重新连接客户端造成的,但在等待我的应用程序的下一个发行计划时无法验证这一点

通过建议(1)使用
Handler
来避免在回调本身中调用
connect()
方法,(2)避免重复使用
LocationClient
对象,只需销毁旧对象并创建新对象即可


我希望这能有所帮助。

您完全正确,在onDisconnected中调用connect()不是一个好主意。我还发现在onConnected()方法中调用requestLocationUpdates()也可能导致一些问题,因此我调用了另一个方法,并在那里进行了请求调用。