2

我在我的应用程序中添加了一个 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),点击项目可以工作并提供预期的结果 - 但如果我添加更多项目,它会在点击时崩溃。(在点击同一项目时,其他项目甚至不可见)

4

4 回答 4

5

在您上次的编辑中,您发布了完整的堆栈跟踪 - 谢谢,这正是我们所需要的。我会忽略“下载磁贴错误”,因为这不是导致您的应用程序崩溃的原因。崩溃发生在“IndexOutOfBoundsException”中。您的问题开始因信息过多而失控 - 让我们将其简化并隔离问题。崩溃发生时您在做什么-您在问题中说双击时会发生这种情况,但异常与 ItemizedIconOverlay 和 onSingleTapUp 有关。一些事情要尝试:

  1. 如果您不使用 ItemizedIconOverlay,它会崩溃吗?
  2. 如果您不向 ItemizedIconOverlay 添加任何项目,它会崩溃吗?
  3. 查看 ItemizedItemOverlay,如果将 onSingleTapUp() 中的所有代码移至 onSingleTapConfirmed() 会怎样。那才是真正属于它的地方。如果您不想从源代码重建 osmdroid,那么您应该能够扩展 ItemizedIconOverlay 并覆盖这两种方法 - 返回 falseonSingleTapUp()并将该方法中的所有旧代码放入onSingleTapConfirmed. 我刚刚为那个Issue 450添加了一张票。
  4. 你说双击放大会崩溃,那你只是点击一个项目呢?那它会崩溃吗?确保在放大时没有添加新项目。

所以 ItemizedOverlay 通过调用size()然后调用getMarker(index)每个项目来工作。出于某种原因,您的 ItemizedOverlay 认为它只有一个项目时有 2 个项目。也许如果你设置了一些断点,特别是在addItem()oraddItems()size(). 也许,连同上面的答案,将提供一些线索!

于 2013-07-15T16:53:53.390 回答
3

您可以尝试覆盖

  • 单点点击
  • onSingleTapConfirmed

这对我有用..

@Override
  public boolean onSingleTapUp(MotionEvent event, MapView mapView){

   return true;
  }
@Override
public boolean onSingleTapConfirmed(MotionEvent event, MapView mapView)
{
    return true;

}
于 2013-12-07T13:39:08.613 回答
0

Your second LogCat's just telling you that slf4j is not configured properly.

Incidentally, you've not posted your full code. If this were your complete source, it wouldn't pass the compile phase, as it's not syntactically correct.

于 2013-07-15T06:53:36.600 回答
0

我想您需要检查谁在修改“MainActivity”。这就是我能说的。如果您使用的是 Eclipse,请密切注意该变量,您可以看到发生了什么

于 2013-07-15T07:28:32.730 回答