我在我的应用程序中添加了一个OSMDroid地图并且工作正常,但是如果我缩小它会崩溃。
我在用户当前位置加载Map时添加一个Item,并在加载地图后在AsyncTask中再加载10个。
更新:工作解决方案
在下面的旧代码中,我在地图上添加了一个“base”-Item,并为地图创建了ItemizedItemOverlay
。然后我加载了更多的OverlayItems并将它们添加到列表中,但它们没有出现在地图上。
现在我在AsyncTask完成加载所有项目之后创建ItemizedItemOverlay
,现在项目全部可见,OnItemSingleTapUp
正在按预期工作,地图上的Double-Tap也适用于那里没有更多的崩溃。我仍然不知道导致崩溃的原因,但它的工作原理如下。
可能的问题:
OverlayItems列表初始化为零对象(显然),但在我添加一个OverlayItem之后,它包含1个OverlayItem + 11个以上的null对象(或null-Refernces?null-Items?正确的术语?)
编辑:没有引起问题,仍然很奇怪。
编辑:我似乎只是在添加更多项目时遇到了崩溃。
这是我的完整代码和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"/>
BIG EDIT:我的大多数问题现在都已解决,但仍然崩溃。
当我双击地图时,它总是会崩溃,但不仅如此。
如果我将clickable=true
添加到XML,它不再崩溃,但我无法移动地图。
Logcat:
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),点击项目可以工作并提供预期结果 - 但是如果我添加更多项目,它会在Tap上崩溃。 (点击相同的项目,其他项目甚至不可见)
答案 0 :(得分:5)
在上一次编辑中,您发布了完整的堆栈跟踪 - 谢谢,这就是我们需要的。我会忽略“错误下载磁贴”,因为这不是崩溃你的应用程序。崩溃发生在“IndexOutOfBoundsException”中。你的问题开始逐渐失控,信息太多 - 让我们简化它并隔离问题。崩溃发生时你在做什么 - 你在问题中说你双击时会发生这种情况,但异常与ItemizedIconOverlay和onSingleTapUp有关。有些事要尝试:
onSingleTapUp()
中返回false并将该方法中的所有旧代码放在onSingleTapConfirmed
中。我刚为Issue 450添加了一张票。因此,ItemizedOverlay通过调用size()
来工作,然后为每个项目调用getMarker(index)
。出于某种原因,您的ItemizedOverlay认为它只有2个项目。也许你设置了一些断点,特别是addItem()
或addItems()
和size()
。也许这一点,以及上面的答案,将提供一些线索!
答案 1 :(得分:3)
您可以尝试覆盖
这对我有用..
@Override
public boolean onSingleTapUp(MotionEvent event, MapView mapView){
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent event, MapView mapView)
{
return true;
}
答案 2 :(得分:0)
您的第二个LogCat只是告诉您slf4j未正确配置。
顺便说一下,您尚未发布完整代码。如果这是你的完整源代码,它将无法通过编译阶段,因为它在语法上不正确。
答案 3 :(得分:0)
我想您需要检查谁在修改“MainActivity”。这就是我所能说的。 如果您正在使用Eclipse,请密切关注该变量,您可以看到正在发生的事情