我有一个扩展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)
答案 0 :(得分:1)
删除此行
dlg.dismiss();
从它现在的位置开始。
或如下所示
runOnUiThread(new Runnable() {
public void run() {
dlg.dismiss();
}
});
最后,如果这也不起作用,那么最好使用异步任务,你可以参考这个LINK
答案 1 :(得分:1)
run() non UI-Thread so you cant dismiss it in run
使用AsyncTask
或Handler
在AsyncTask ....
onpreExecute()= ProgressDialog dialog = new ProgressDialog(this);dialog.show();
inBackground(...)=put your run code here .....
onpostExecute =dialog.dismiss();