【问题标题】:java.util.ConcurrentModificationException with Google Play Services LocationClientjava.util.ConcurrentModificationException 与 Google Play 服务 LocationClient
【发布时间】:2013-09-28 19:44:11
【问题描述】:

我在 Android 中使用 Google Play 服务 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 方法只是为了确保该对象不为空:

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
}

【问题讨论】:

    标签: java android google-play-services concurrentmodification


    【解决方案1】:

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

    我怀疑问题是由于我的应用程序尝试以 onDisconnected() 方法重新连接客户端,但在等待我的应用程序的下一个发布计划时无法验证。

    Another stackoverflow article 通过建议 (1) 使用 Handler 来避免在回调本身中调用 connect() 方法,给了我很好的提示,(2) 避免重复使用 LocationClient 对象并销毁旧对象一个并创建一个新的。

    希望对您有所帮助。

    【讨论】:

    • 你说得对,在 onDisconnected 中调用 connect() 不是一个好主意。我还发现在 onConnected() 方法中调用 requestLocationUpdates() 也会导致一些问题,所以我改为调用另一个方法并在那里进行请求调用。
    猜你喜欢
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 2016-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    相关资源
    最近更新 更多