0

以下是问题的摘要:

我有一个名为 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();
}

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

4

1 回答 1

0

java.util.ConcurrentModificationException异常中可以清楚地看出,在一个线程中,您的程序正在修改ArrayList(即您正在添加 new Overlay),而同一列表正在由一个单独的线程迭代(即调用mapView.refreshDrawableState()以强制 View 重新绘制,这反过来又遍历添加的覆盖列表)- 根据 Java 文档,这是不允许的,参考:ConcurrentModificationException

所以你可以尝试以这样的方式重写你的代码,首先添加所有可用的覆盖,然后调用mapView.refreshDrawableState()

于 2012-12-10T10:56:58.980 回答