OSMDroid выходит из строя, когда я касаюсь представления во время его анимации

#android #kotlin #osmdroid

#Android #котлин #осмдроид

Вопрос:

Мое приложение аварийно завершает работу, когда вызывается MapController.animateTo и происходит какое-либо событие касания, пока анимация все еще выполняется. Это урезанная версия того, что я делаю. Но этот код также аварийно завершает работу. Сначала я подумал, что это метод mLocationOverlay.runOnFirstFix. Но я обнаружил, что могу позвонить ему куда угодно, и он все равно вылетит с исключением NullPointerException

 val ctx = applicationContext  Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx))    map = findViewByIdlt;Viewgt;(R.id.map) as MapView  map.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE)  mapController = MapController(map)   mLocationOverlay = MyLocationNewOverlay(GpsMyLocationProvider(this), map)  mLocationOverlay.enableMyLocation()  mLocationOverlay.runOnFirstFix {  runOnUiThread {  mapController.animateTo(mLocationOverlay.myLocation, 14.0, 4000)  }  }  map.overlays.add(mLocationOverlay)  

 E/InputEventReceiver: Exception dispatching input event. E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.animation.Animator.cancel()' on a null object reference  at org.osmdroid.views.MapController.stopAnimation(MapController.java:259)  at org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay.disableFollowLocation(MyLocationNewOverlay.java:406)  at org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay.onTouchEvent(MyLocationNewOverlay.java:301)  at org.osmdroid.views.overlay.DefaultOverlayManager.onTouchEvent(DefaultOverlayManager.java:247)  at org.osmdroid.views.MapView.dispatchTouchEvent(MapView.java:1123)  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3920)  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3535)  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3920)  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3535)  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3920)  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3535)  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3920)  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3535)  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3920)  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3535)  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3920)  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3535)  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3920)  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3535)  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3920)  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3535)  at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:913)  at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1957)  at android.app.Activity.dispatchTouchEvent(Activity.java:4182)  at androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)  at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:871)  at android.view.View.dispatchPointerEvent(View.java:15458)  at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:7457)  at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:7233)  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6595)  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:6652)  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:6618)  at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:6786)  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:6626)  at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:6843)  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6599)  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:6652)  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:6618)  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:6626)  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6599)  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:9880)  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:9718)  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:9671)  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:10014)  at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:220)  at android.os.MessageQueue.nativePollOnce(Native Method)  at android.os.MessageQueue.next(MessageQueue.java:335)  at android.os.Looper.loop(Looper.java:206)  at android.app.ActivityThread.main(ActivityThread.java:8633)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION: main  Process: com.openmapskotlinexample, PID: 7652  java.lang.NullPointerException: Attempt to invoke virtual method 'void android.animation.Animator.cancel()' on a null object reference  at org.osmdroid.views.MapController.stopAnimation(MapController.java:259)  at org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay.disableFollowLocation(MyLocationNewOverlay.java:406)  at org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay.onTouchEvent(MyLocationNewOverlay.java:301)  at org.osmdroid.views.overlay.DefaultOverlayManager.onTouchEvent(DefaultOverlayManager.java:247)  at org.osmdroid.views.MapView.dispatchTouchEvent(MapView.java:1123)  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3920)  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3535)  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3920)  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3535)  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3920)  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3535)  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3920)  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3535)  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3920)  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3535)  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3920)  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3535)  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3920)  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3535)  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3920)  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3535)  at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:913)  at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1957)  at android.app.Activity.dispatchTouchEvent(Activity.java:4182)  at androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)  at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:871)  at android.view.View.dispatchPointerEvent(View.java:15458)  at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:7457)  at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:7233)  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6595)  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:6652)  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:6618)  at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:6786)  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:6626)  at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:6843)  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6599)  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:6652)  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:6618)  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:6626)  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6599)  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:9880)  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:9718)  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:9671)  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:10014)  at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:220)  at android.os.MessageQueue.nativePollOnce(Native Method)  at android.os.MessageQueue.next(MessageQueue.java:335)  at android.os.Looper.loop(Looper.java:206)  at android.app.ActivityThread.main(ActivityThread.java:8633)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)  

Ответ №1:

Конечно, как только я задам этот вопрос, я все пойму.

 mLocationOverlay.runOnFirstFix {  runOnUiThread {  mapView.controller.animateTo(mLocationOverlay.myLocation, 14.0, 4000)  //mapController.animateTo(mLocationOverlay.myLocation, 14.0, 4000)  }  }  

Я должен вызвать метод animateTo через MapView, а не пытаться вызвать его напрямую из MapController