其他分享
首页 > 其他分享> > 缩小时OSMDroid崩溃

缩小时OSMDroid崩溃

作者:互联网

我已经在我的应用程序中添加了OSMDroid地图,它工作正常,但是如果我缩小地图,它会崩溃.

我在将地图加载到用户当前位置时添加了一个项目,并在加载地图后在AsyncTask中加载了另外10个项目.

更新:工作解决方案

在下面的旧代码中,我在用户位置的地图上添加了一个“基本”项,并为地图创建了ItemizedItemOverlay.然后,我加载了更多OverlayItems并将其添加到列表中,但是它们没有出现在地图上.

现在,我在AsyncTask完成所有项目的加载之后创建ItemizedItemOverlay,现在所有项目都可见,OnItemSingleTapUp可以按预期工作,在地图上双击也可以正常工作,并且不再崩溃.我仍然不太清楚是什么原因导致了崩溃,但是它的工作原理是这样的.

可能的问题:
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"/>

大编辑:我的大多数问题现在已解决,但仍然崩溃.

当我双击地图时,它总是崩溃,但不仅如此.
如果我将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上崩溃. (在同一项目的Tap上,其他的甚至都不可见)

解决方法:

在上一次编辑中,您发布了完整的stacktrace-谢谢,这就是我们所需要的.我会忽略“下载瓷砖时出错”,因为那不是崩溃您的应用程序的原因.崩溃发生在“ IndexOutOfBoundsException”中.您的问题开始因过多的信息而变得一发不可收拾-我们将其简化并找出问题所在.当崩溃发生时您在做什么-您在问题中说双击时会发生崩溃,但该异常与ItemizedIconOverlay和onSingleTapUp有关.可以尝试的一些事情:

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

因此,ItemizedOverlay通过调用size()进行工作,然后为每个项目调用getMarker(index).由于某种原因,您的ItemizedOverlay认为只有2个项目时才有2个项目.也许您设置了一些断点,尤其是在addItem()或addItems()和size()中.也许,与上述答案一起,将提供一些线索!

标签:android,osmdroid
来源: https://codeday.me/bug/20191011/1890844.html