我正在使用MapView,它需要添加多个覆盖项目。我发现添加覆盖项目速度很慢,导致地图拖到ANR时。所以我构建了一个AsyncTask来添加重叠项。最初,我发现它一直失败,因为我从后台线程访问覆盖集合,并且我收集它不是线程安全的。所以我改变了它,所以覆盖只是从UI线程改变。它现在可以工作,但只有大部分时间。当地图被触摸时,它偶尔会崩溃。如何从AsyncTask更新MapView覆盖项目而无ArrayIndexOutOfBoundsException
这里的的AsyncTask(我MapView类的子类中的内部类):
class showItemsTask extends AsyncTask<Void, User, Void> {
public boolean stop = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected Void doInBackground(Void... v) {
User[] items = Item.getItemList();
if (items != null && items.length > 0) {
int i = 0;
for (User item : items) {
publishProgress(item);
i++;
if (stop || i>MAX_NUMBER_OF_ITEMS) break;
}
stop = false;
}
return null;
}
@Override
protected void onProgressUpdate(User... itemContainer) {
super.onProgressUpdate(itemContainer);
User item = itemContainer[0];
showItem(item.location.latitude, item.location.longitude, item.location.firstname, ((Integer) item.location.id).toString());
}
public void showItem(float latitude, float longitude, String itemTitle, String itemSubtitle) {
try {
GeoPoint point = new GeoPoint((int) (latitude * 1000000), (int) (longitude * 1000000));
OverlayItem marker = new OverlayItem(point, itemTitle, itemSubtitle);
availableItemsOverlay.addOverlay(marker);
} catch (Exception e) {
Trace.e(TAG, "Exception drawing a item");
}
}
protected void onPostExecute(Void v) {
invalidate();
}
}
这里的堆栈跟踪:
0 java.lang.ArrayIndexOutOfBoundsException
1 at com.google.android.maps.ItemizedOverlay.maskHelper(ItemizedOverlay.java:562)
2 at com.google.android.maps.ItemizedOverlay.setFocus(ItemizedOverlay.java:365)
3 at com.google.android.maps.ItemizedOverlay.focus(ItemizedOverlay.java:539)
4 at com.google.android.maps.ItemizedOverlay.onTap(ItemizedOverlay.java:455)
5 at com.google.android.maps.OverlayBundle.onTap(OverlayBundle.java:83)
我要去下来的AsyncTask在错误的道路?如果没有,你能看到为什么我得到这个异常,当在UI线程中进行覆盖的所有更改时?
您是否碰巧了解mapView未失效的确切原因? 我有类似的问题:http://stackoverflow.com/questions/23011264/mapview-doesnt-invalidate-onprogressupdated-of-async-task – zIronManBox 2014-04-14 05:58:06