0

我有一个扩展 MapActivity 的类。

我使用名为 updateMap 的函数在地图上放置了大头针。大头针放置在基于加载的巴士站列表的位置。

我想在下载停止数组时显示一个加载对话框,但我的代码出现错误。我目前弹出对话框并生成一个线程来加载公交路线并更新地图。

经过一些研究,我开始意识到 UI 层不能从单独的线程更新。我试过移动东西,但似乎没有任何效果。

任何帮助表示赞赏!

private void updateMap(final int selectedRoute, final boolean isMajor, final boolean isMinor)
{
    dlg = ProgressDialog.show(TransitMap.this, "Updating Map", "Processing... Please wait...");
    new Thread()
    {
        public void run()
        {
            try 
            {
                TransitMap.this.removePins();
                TransitMap.this.addRoutePins(selectedRoute, isMajor, isMinor);
                TransitMap.this.addBusPins(selectedRoute);

                map.post(new Runnable()
                {   
                    public void run()
                    {
                        TransitMap.this.map.postInvalidate();
                    }
                });
            }
            catch(Exception ex)
            {
        }
            // processing done, hide progress dialog
            dlg.dismiss();
        }
    }.start();
}

修改类以使用 AsyncTask 后,我的程序仍然崩溃!

这是 logCat 错误

04-13 15:16:49.217: E/AndroidRuntime(21898): FATAL EXCEPTION: main
04-13 15:16:49.217: E/AndroidRuntime(21898): java.util.ConcurrentModificationException
04-13 15:16:49.217: E/AndroidRuntime(21898):    at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:576)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:44)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at com.google.android.maps.MapView.onDraw(MapView.java:530)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.view.View.draw(View.java:6971)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.view.ViewGroup.drawChild(ViewGroup.java:1710)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1437)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.view.ViewGroup.drawChild(ViewGroup.java:1708)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1437)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.view.View.draw(View.java:6974)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.widget.FrameLayout.draw(FrameLayout.java:357)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.view.ViewGroup.drawChild(ViewGroup.java:1710)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1437)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.view.ViewGroup.drawChild(ViewGroup.java:1708)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1437)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.view.ViewGroup.drawChild(ViewGroup.java:1708)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1437)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.view.ViewGroup.drawChild(ViewGroup.java:1708)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1437)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.view.ViewGroup.drawChild(ViewGroup.java:1708)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1437)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.view.View.draw(View.java:6974)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.widget.FrameLayout.draw(FrameLayout.java:357)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.view.ViewGroup.drawChild(ViewGroup.java:1710)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1437)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.view.View.draw(View.java:6974)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.widget.FrameLayout.draw(FrameLayout.java:357)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1929)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.view.ViewRoot.draw(ViewRoot.java:1590)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1290)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1939)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.os.Looper.loop(Looper.java:143)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at android.app.ActivityThread.main(ActivityThread.java:4196)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at java.lang.reflect.Method.invokeNative(Native Method)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at java.lang.reflect.Method.invoke(Method.java:507)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-13 15:16:49.217: E/AndroidRuntime(21898):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
04-13 15:16:49.217: E/AndroidRuntime(21898):    at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

1

删除此行

dlg.dismiss();

从它现在所在的地方开始,然后放在它下面。

或如下所示

runOnUiThread(new Runnable() {
    public void run() {
        dlg.dismiss();
    }
});

最后,如果这也不起作用,那么最好使用异步任务,您可以参考此链接

于 2012-04-13T18:33:54.030 回答
1

run() non UI-Thread so you cant dismiss it in run使用AsyncTaskHandler

在 AsyncTask ....

onpreExecute()= ProgressDialog dialog = new ProgressDialog(this);dialog.show(); 
inBackground(...)=put your run code here ..... 
onpostExecute =dialog.dismiss();
于 2012-04-13T18:34:29.040 回答