移动谷歌地图导致错误?

时间:2012-12-10 08:44:04

标签: java android multithreading google-maps android-layout

以下是问题摘要:

我有一个名为BusLocator的类,它跟踪总线的位置。 现在我从文本文件中获取输入信息。

程序启动正常,然后当总线到达屏幕末端时,或者当我尝试查看其他内容时,它会随机停止。

以下是我的错误:

12-10 08:28:43.014: E/AndroidRuntime(15257): FATAL EXCEPTION: main
12-10 08:28:43.014: E/AndroidRuntime(15257): java.util.ConcurrentModificationException
12-10 08:28:43.014: E/AndroidRuntime(15257):    at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:569)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:41)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at com.google.android.maps.MapView.onDraw(MapView.java:532)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at android.view.View.draw(View.java:13707)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at android.view.View.draw(View.java:13591)
 12-10 08:28:43.014: E/AndroidRuntime(15257):   at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at android.view.View.draw(View.java:13589)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at android.view.View.draw(View.java:13710)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at android.widget.FrameLayout.draw(FrameLayout.java:467)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at android.view.View.draw(View.java:13591)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at android.view.View.draw(View.java:13589)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at  android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at android.view.View.draw(View.java:13710)
 12-10 08:28:43.014: E/AndroidRuntime(15257):   at android.widget.FrameLayout.draw(FrameLayout.java:467)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at  com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2211)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:2281)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at android.view.ViewRootImpl.draw(ViewRootImpl.java:2177)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2045)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1854)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at  android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at    android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at android.view.Choreographer.doFrame(Choreographer.java:532)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at android.os.Handler.handleCallback(Handler.java:725)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at android.os.Looper.loop(Looper.java:137)
 12-10 08:28:43.014: E/AndroidRuntime(15257):   at    android.app.ActivityThread.main(ActivityThread.java:5039)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at java.lang.reflect.Method.invokeNative(Native Method)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at java.lang.reflect.Method.invoke(Method.java:511)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-10 08:28:43.014: E/AndroidRuntime(15257):    at dalvik.system.NativeStart.main(Native Method)
12-10 08:28:45.464: D/dalvikvm(15257): GC_CONCURRENT freed 1479K, 21% free 6387K/7988K, paused 18ms+214ms, total 568ms
12-10 08:28:46.034: I/Process(15257): Sending signal. PID: 15257 SIG: 9

读取总线坐标的代码在一个单独的线程上运行,如下所示:

    class BusThread extends Thread {
private boolean run = true;
private Context context;
    BusLocator bus;

public BusThread(Context context,BusLocator bl) {
    this.context = context;
    this.bus=bl;
}

public void negateRun() {
    run = false;
}

public void run() {
    try {
        AssetManager am = context.getAssets();
        InputStream is = am.open("test.txt");
        Scanner scan = new Scanner(is);
        double longitude;
        double latitude;
        while (run) {
            if (scan.hasNext()) {
                latitude = scan.nextDouble();
                longitude = scan.nextDouble();
                bus.updateBusLoc(longitude,latitude);
                //Pause for 2 seconds
                Thread.sleep(2000);
            }
            else
                scan.reset();
        }

    } catch (IOException ioe) {

        ioe.printStackTrace();
        return;
    } catch (InterruptedException ie) {
        // TODO Auto-generated catch block
        ie.printStackTrace();
        return;
    }
}

}

最后,我的错误可能来自哪里,虽然我不知道为什么:

public void updateBusLoc(double longi,double lati){
    if(!onScreen){
        onScreen=true;
        GeoPoint point = new GeoPoint((int)(lati*1E6),(int)(longi*1E6));
    busOverlay.addOverlay(new OverlayItem(point, busTitle, busText));
    mapView.getOverlays().add(busOverlay);
    }
    else
    {
        GeoPoint point = new GeoPoint((int)(lati*1E6),(int)(longi*1E6));
        busOverlay.replaceLastOverlay(new OverlayItem(point,busTitle, busText));
        mapView.getOverlays().add(busOverlay);
    }
    mapView.refreshDrawableState();
}

我变得非常绝望......现在看几个小时。任何帮助都将非常感激!

1 个答案:

答案 0 :(得分:0)

java.util.ConcurrentModificationException异常非常清楚,在一个线程中,您的程序正在修改ArrayList(即您正在添加新的Overlay),而同一个列表正在单独迭代线程(即调用mapView.refreshDrawableState()强制View重新绘制,后者又遍历添加的叠加列表) - 根据Java文档不允许,参考:ConcurrentModificationException

所以您可以尝试重新编写代码,首先添加所有可用的叠加层,然后调用mapView.refreshDrawableState()