MapActivity 中的 ConcurrentModificationException

发布于 2024-10-07 00:41:16 字数 1556 浏览 0 评论 0原文

这是我的第一个问题,所以如果有什么问题,请告诉我。

现在,我有一个 public class MainMap extends MapActivity ,它使用 MyLocationListener 作为用户的位置,并使用 HelloItemizedOverlay。 我从 XML 文件中获取某些 POI,我想添加标记(非常基本的叠加)。

用户按下按钮,AsyncTask 启动:

private class GetPOITask extends AsyncTask<String, Void, Void> {

 protected Void doInBackground(String... params) {
    clearMapOverlay();
    if(!isBusyAddingOverlays)
    {
       connect(params[0]);
    }
    return null;
 }

 protected void onPreExecute() {
  clearMapOverlay();
 }
}

清除 MapOverlay(this.mv 是 MapView):

public void clearMapOverlay()
 {
  this.mv.getOverlays().remove(placesItemizedOverlay);
  this.mv.postInvalidate();
  this.mv.getOverlays().add(myLoc);
 }

这是当我有足够的信息来添加标记时我的连接方法调用的方法:

private void addOverlayToList(int id, GeoPoint point, String title, String description)
{
 //Log.e("Debug", "Adding overlay");
 placesItemizedOverlay.addOverlay(new POIOverlay(id, point, title, description));
 this.mv.getOverlays().add(placesItemizedOverlay);
}

这应该是所有相关代码。

问题是,当我添加 POI 并缩放地图(或移动地图)时,我收到一个 ConcurrentModificationException

我搜索过这个网站,阅读类似的线程(这个例如)但我真的找不到我做错了什么。

了解我自己,这可能是显而易见的事情。

提前致谢,

伊尔温

this is my first question here, so if anything's wrong, do tell.

Now, I have a public class MainMap extends MapActivity which uses a MyLocationListener for the user's location and a HelloItemizedOverlay.
I'm getting certain POI's from an XML-file which I want to add markers (pretty basic Overlays).

User presses a button, AsyncTask starts:

private class GetPOITask extends AsyncTask<String, Void, Void> {

 protected Void doInBackground(String... params) {
    clearMapOverlay();
    if(!isBusyAddingOverlays)
    {
       connect(params[0]);
    }
    return null;
 }

 protected void onPreExecute() {
  clearMapOverlay();
 }
}

Clearing out the MapOverlay (this.mv is a MapView):

public void clearMapOverlay()
 {
  this.mv.getOverlays().remove(placesItemizedOverlay);
  this.mv.postInvalidate();
  this.mv.getOverlays().add(myLoc);
 }

This is the method that my connect-method calls when I have enough information to add a marker:

private void addOverlayToList(int id, GeoPoint point, String title, String description)
{
 //Log.e("Debug", "Adding overlay");
 placesItemizedOverlay.addOverlay(new POIOverlay(id, point, title, description));
 this.mv.getOverlays().add(placesItemizedOverlay);
}

That should be all the relevant code.

The problem is that when I add the POI's and zoom the map (or move it), I get a ConcurrentModificationException.

I've searched through this site, reading similar threads (this one for example) but I can't really find what I'm doing wrong.

Knowing myself, it's probably something obvious.

Thanks in advance,

iarwain

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷子口的你 2024-10-14 00:41:16

这可能是由于 addOverlayToList() 是从 connect() 调用的,而 connect() 是在不同线程 (doInBackground()) 中调用的。只有启动 UI 的线程才能触摸其视图。

将添加覆盖调用移至 GetPOITaskonPostExecute()(您应该覆盖它)。

This could come from the fact that addOverlayToList() is called from connect() which is called in a different Thread (doInBackground()). Only the thread wich initiated the UI can touch its views.

Move the add overlay calls into onPostExecute() (which you should override) of GetPOITask.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文