我在我的应用程序中添加了一个 OSMDroid 地图,它工作正常,但如果我缩小,它会崩溃。
我在用户当前位置加载地图时添加了一个项目,并在加载地图后在 AsyncTask 中再加载 10 个项目。
更新:工作解决方案
在下面的旧代码中,我在用户位置的地图中添加了一个“基础”项,并ItemizedItemOverlay
为地图创建了一个。然后我加载了更多 OverlayItems 并将它们添加到列表中,但它们没有出现在地图上。
现在我ItemizedItemOverlay
在 AsyncTask 完成加载所有项目后创建,现在所有项目都可见,OnItemSingleTapUp
按预期工作,在地图上双击也能正常工作,不再有崩溃。我仍然不知道是什么导致了崩溃,但它是这样工作的。
可能的问题:
OverlayItems 列表用零个对象初始化(显然),但是在我添加一个 OverlayItem 后,它包含 1 个 OverlayItem + 11 个空对象(或空引用?空项目?什么是正确的术语? )
编辑:没有引起问题,仍然很奇怪。
编辑:似乎我只会在添加更多项目时崩溃。
这是我的完整代码和 Logcat:
mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
mapView.setTileSource(TileSourceFactory.MAPNIK);
mapView.setBuiltInZoomControls(true);
mapView.setMultiTouchControls(true);
mapController = this.mapView.getController();
mapController.setZoom(25);
GeoPoint center = new GeoPoint(DataManager.glat, DataManager.glon);
mapController.setCenter(center);
mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 100,
this);
items = new ArrayList<OverlayItem>(); // items now null
items.add(new OverlayItem("Here", "SampleDescription", center));
// items now contains 1 OverlayItem and 11 null-items
this.mLocationOverlay = new ItemizedIconOverlay<OverlayItem>(items,
new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
@Override
public boolean onItemSingleTapUp(final int index,
final OverlayItem item) {
Intent intent=new Intent();
intent.putExtra("newShopName", item.mTitle);
intent.putExtra("newShopAdd", item.mDescription);
setResult(RESULT_OK, intent);
finish();
return true;
}
@Override
public boolean onItemLongPress(final int index,
final OverlayItem item) {
toast = Toast.makeText(ShopChooseActivity.this, item.mTitle + ", " + item.mDescription, Toast.LENGTH_LONG);
toast.show();
return false;
}
}, mResourceProxy);
this.mapView.getOverlays().add(this.mLocationOverlay);
mapView.invalidate();
这里我称之为 AsyncTask:
loadMap = new LoadChooseShop(ShopChooseActivity.this, items).execute();
在这里我“刷新”了地图:
@Override
public void onTaskCompleted(int returncode) {
try {
items = loadMap.get();
if(items != null)
mapView.invalidate();
else {
Toast.makeText(getApplicationContext(), R.string.error_connectionfailure , Toast.LENGTH_LONG).show();
setResult(RESULT_CANCELED);
finish();
}
} catch (InterruptedException e) {
} catch (ExecutionException e) {
}
}
和 XML:
<org.osmdroid.views.MapView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
大编辑:我的大部分问题现在都解决了,它仍然崩溃。
当我双击地图时,它总是崩溃,但不仅如此。如果我添加clickable=true
到 XML,它不会再崩溃,但我不能移动地图。
日志猫:
07-15 14:41:16.339: E/InputEventReceiver(17302): Exception dispatching input event.
07-15 14:41:16.339: E/MessageQueue-JNI(17302): Exception in MessageQueue callback: handleReceiveCallback
07-15 14:41:16.404: E/MessageQueue-JNI(17302): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at java.util.ArrayList.get(ArrayList.java:304)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at org.osmdroid.views.overlay.ItemizedOverlay.getItem(ItemizedOverlay.java:157)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at org.osmdroid.views.overlay.ItemizedIconOverlay.activateSelectedItems(ItemizedIconOverlay.java:170)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at org.osmdroid.views.overlay.ItemizedIconOverlay.onSingleTapUp(ItemizedIconOverlay.java:117)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at org.osmdroid.views.overlay.OverlayManager.onSingleTapUp(OverlayManager.java:291)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at org.osmdroid.views.MapView$MapViewGestureDetectorListener.onSingleTapUp(MapView.java:1539)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at android.view.GestureDetector.onTouchEvent(GestureDetector.java:1344)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at org.osmdroid.views.MapView.dispatchTouchEvent(MapView.java:904)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2115)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1468)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at android.app.Activity.dispatchTouchEvent(Activity.java:2487)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2063)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at android.view.View.dispatchPointerEvent(View.java:7520)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3376)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3308)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4402)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4380)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4484)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at android.os.MessageQueue.nativePollOnce(Native Method)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at android.os.MessageQueue.next(MessageQueue.java:125)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at android.os.Looper.loop(Looper.java:124)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at android.app.ActivityThread.main(ActivityThread.java:4898)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at java.lang.reflect.Method.invoke(Method.java:511)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
07-15 14:41:16.404: E/MessageQueue-JNI(17302): at dalvik.system.NativeStart.main(Native Method)
07-15 14:41:21.914: D/AndroidRuntime(17302): Shutting down VM
07-15 14:41:21.914: W/dalvikvm(17302): threadid=1: thread exiting with uncaught exception (group=0x417cc2a0)
07-15 14:41:21.984: E/AndroidRuntime(17302): FATAL EXCEPTION: main
07-15 14:41:21.984: E/AndroidRuntime(17302): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
07-15 14:41:21.984: E/AndroidRuntime(17302): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
07-15 14:41:21.984: E/AndroidRuntime(17302): at java.util.ArrayList.get(ArrayList.java:304)
07-15 14:41:21.984: E/AndroidRuntime(17302): at org.osmdroid.views.overlay.ItemizedOverlay.getItem(ItemizedOverlay.java:157)
07-15 14:41:21.984: E/AndroidRuntime(17302): at org.osmdroid.views.overlay.ItemizedIconOverlay.activateSelectedItems(ItemizedIconOverlay.java:170)
07-15 14:41:21.984: E/AndroidRuntime(17302): at org.osmdroid.views.overlay.ItemizedIconOverlay.onSingleTapUp(ItemizedIconOverlay.java:117)
07-15 14:41:21.984: E/AndroidRuntime(17302): at org.osmdroid.views.overlay.OverlayManager.onSingleTapUp(OverlayManager.java:291)
07-15 14:41:21.984: E/AndroidRuntime(17302): at org.osmdroid.views.MapView$MapViewGestureDetectorListener.onSingleTapUp(MapView.java:1539)
07-15 14:41:21.984: E/AndroidRuntime(17302): at android.view.GestureDetector.onTouchEvent(GestureDetector.java:1344)
07-15 14:41:21.984: E/AndroidRuntime(17302): at org.osmdroid.views.MapView.dispatchTouchEvent(MapView.java:904)
07-15 14:41:21.984: E/AndroidRuntime(17302): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416)
07-15 14:41:21.984: E/AndroidRuntime(17302): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159)
07-15 14:41:21.984: E/AndroidRuntime(17302): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416)
07-15 14:41:21.984: E/AndroidRuntime(17302): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159)
07-15 14:41:21.984: E/AndroidRuntime(17302): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416)
07-15 14:41:21.984: E/AndroidRuntime(17302): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159)
07-15 14:41:21.984: E/AndroidRuntime(17302): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416)
07-15 14:41:21.984: E/AndroidRuntime(17302): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159)
07-15 14:41:21.984: E/AndroidRuntime(17302): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416)
07-15 14:41:21.984: E/AndroidRuntime(17302): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159)
07-15 14:41:21.984: E/AndroidRuntime(17302): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416)
07-15 14:41:21.984: E/AndroidRuntime(17302): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159)
07-15 14:41:21.984: E/AndroidRuntime(17302): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2115)
07-15 14:41:21.984: E/AndroidRuntime(17302): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1468)
07-15 14:41:21.984: E/AndroidRuntime(17302): at android.app.Activity.dispatchTouchEvent(Activity.java:2487)
07-15 14:41:21.984: E/AndroidRuntime(17302): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2063)
07-15 14:41:21.984: E/AndroidRuntime(17302): at android.view.View.dispatchPointerEvent(View.java:7520)
07-15 14:41:21.984: E/AndroidRuntime(17302): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3376)
07-15 14:41:21.984: E/AndroidRuntime(17302): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3308)
07-15 14:41:21.984: E/AndroidRuntime(17302): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4402)
07-15 14:41:21.984: E/AndroidRuntime(17302): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4380)
07-15 14:41:21.984: E/AndroidRuntime(17302): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4484)
07-15 14:41:21.984: E/AndroidRuntime(17302): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
07-15 14:41:21.984: E/AndroidRuntime(17302): at android.os.MessageQueue.nativePollOnce(Native Method)
07-15 14:41:21.984: E/AndroidRuntime(17302): at android.os.MessageQueue.next(MessageQueue.java:125)
07-15 14:41:21.984: E/AndroidRuntime(17302): at android.os.Looper.loop(Looper.java:124)
07-15 14:41:21.984: E/AndroidRuntime(17302): at android.app.ActivityThread.main(ActivityThread.java:4898)
07-15 14:41:21.984: E/AndroidRuntime(17302): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 14:41:21.984: E/AndroidRuntime(17302): at java.lang.reflect.Method.invoke(Method.java:511)
07-15 14:41:21.984: E/AndroidRuntime(17302): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
07-15 14:41:21.984: E/AndroidRuntime(17302): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
07-15 14:41:21.984: E/AndroidRuntime(17302): at dalvik.system.NativeStart.main(Native Method)
更新: 如果我不添加更多项目(不要调用 AsyncTask),点击项目可以工作并提供预期的结果 - 但如果我添加更多项目,它会在点击时崩溃。(在点击同一项目时,其他项目甚至不可见)