简体   繁体   中英

moving google maps causes error?

Here is a summary of the problem:

I have a class called BusLocator which tracks the location of a bus. Right now I'm getting my input from a text file.

The program starts off fine, then it will randomly stop when the bus reaches the end of the screen, or when I try to see something else.

Here are my errors:

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

The code which reads the coordinates of the bus runs on a separate thread, which is the following:

    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;
    }
}

}

and lastly, where my error may be coming from, although I wouldnt know why:

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();
}

I'm getting pretty desparate... Looking at this for a few hours now. Any help would be most appreciated!

It is very clear from java.util.ConcurrentModificationException exception that in one thread your program is modifying the ArrayList (ie you are adding new Overlay ) while the same list is being iterated by a separate thread (ie calling mapView.refreshDrawableState() to force a View to re-draw, which in turn iterate through the list of added overlays)- Which as per Java docs is not allowed, ref: ConcurrentModificationException

So what you can try is to re-write your code in such a way that, first add all available overlays and then call mapView.refreshDrawableState()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM