MapActivity 中的 ConcurrentModificationException
这是我的第一个问题,所以如果有什么问题,请告诉我。
现在,我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是由于
addOverlayToList()
是从connect()
调用的,而connect()
是在不同线程 (doInBackground()
) 中调用的。只有启动 UI 的线程才能触摸其视图。将添加覆盖调用移至
GetPOITask
的onPostExecute()
(您应该覆盖它)。This could come from the fact that
addOverlayToList()
is called fromconnect()
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) ofGetPOITask
.