VelocityTracker导致Android 4.4崩溃

在我的应用程序中,我有一个抽屉布局的自定义实现。 它在Android 2.3和4.0.4上运行良好,但在Android 4.4上,几乎每次打开或closures抽屉时都会崩溃。 这里是错误日志:

E/AndroidRuntime( 9839): FATAL EXCEPTION: main E/AndroidRuntime( 9839): Process: com.andryr.launcher, PID: 9839 E/AndroidRuntime( 9839): java.lang.IllegalStateException: Already in the pool! E/AndroidRuntime( 9839): at android.util.Pools$SimplePool.release(Pools.java:112) E/AndroidRuntime( 9839): at android.util.Pools$SynchronizedPool.release(Pools.java:161) E/AndroidRuntime( 9839): at android.view.VelocityTracker.recycle(VelocityTracker.java:85) E/AndroidRuntime( 9839): at com.andryr.widget.DrawerLayout.onTouchEvent(DrawerLayout.java:131) E/AndroidRuntime( 9839): at android.view.View.dispatchTouchEvent(View.java:7706) E/AndroidRuntime( 9839): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2210) E/AndroidRuntime( 9839): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945) E/AndroidRuntime( 9839): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216) E/AndroidRuntime( 9839): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959) E/AndroidRuntime( 9839): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216) E/AndroidRuntime( 9839): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959) E/AndroidRuntime( 9839): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216) E/AndroidRuntime( 9839): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959) E/AndroidRuntime( 9839): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216) E/AndroidRuntime( 9839): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959) E/AndroidRuntime( 9839): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068) E/AndroidRuntime( 9839): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515) E/AndroidRuntime( 9839): at android.app.Activity.dispatchTouchEvent(Activity.java:2458) E/AndroidRuntime( 9839): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016) E/AndroidRuntime( 9839): at android.view.View.dispatchPointerEvent(View.java:7886) E/AndroidRuntime( 9839): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3947) E/AndroidRuntime( 9839): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3826) E/AndroidRuntime( 9839): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392) E/AndroidRuntime( 9839): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3442) E/AndroidRuntime( 9839): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3411) E/AndroidRuntime( 9839): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3518) E/AndroidRuntime( 9839): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3419) E/AndroidRuntime( 9839): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3575) E/AndroidRuntime( 9839): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392) E/AndroidRuntime( 9839): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3442) E/AndroidRuntime( 9839): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3411) E/AndroidRuntime( 9839): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3419) E/AndroidRuntime( 9839): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392) E/AndroidRuntime( 9839): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5532) E/AndroidRuntime( 9839): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5512) E/AndroidRuntime( 9839): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5483) E/AndroidRuntime( 9839): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5612) E/AndroidRuntime( 9839): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185) E/AndroidRuntime( 9839): at android.os.MessageQueue.nativePollOnce(Native Method) E/AndroidRuntime( 9839): at android.os.MessageQueue.next(MessageQueue.java:138) E/AndroidRuntime( 9839): at android.os.Looper.loop(Looper.java:123) E/AndroidRuntime( 9839): at android.app.ActivityThread.main(ActivityThread.java:5001) E/AndroidRuntime( 9839): at java.lang.reflect.Method.invoke(Native Method) E/AndroidRuntime( 9839): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) E/AndroidRuntime( 9839): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 

这里是我的代码:

 @Override public boolean onInterceptTouchEvent(MotionEvent ev) { if(!mInterceptTouchEvent) { return false; } if (ev.getAction() == MotionEvent.ACTION_DOWN && ((ev.getX() < ViewConfiguration.get(getContext()) .getScaledEdgeSlop() && !mExpanded) || (ev.getX() > getWidth() - ViewConfiguration.get(getContext()) .getScaledEdgeSlop()) && mExpanded)) { if (mVelocityTracker == null) mVelocityTracker = VelocityTracker.obtain(); else mVelocityTracker.clear(); return true; } return false; } @Override public boolean onTouchEvent(MotionEvent ev) { if(!mInterceptTouchEvent) { return false; } mLastMotionX = ev.getX(); if (mVelocityTracker == null) return false; switch (ev.getAction()) { case MotionEvent.ACTION_MOVE: mVelocityTracker.addMovement(ev); mVelocityTracker.computeCurrentVelocity(1000); mVelocity = mVelocityTracker.getXVelocity(); // ViewHelper.setTranslationX(mDrawerLayout, // -mDrawerLayout.getWidth() // + mLastMotionX); // ViewHelper.setTranslationX(mRootView, mLastMotionX); mPosition = (int) (-mDrawerLayout.getWidth() + mLastMotionX); requestLayout(); break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: if (Math.abs(mVelocity) > ViewConfiguration.get(getContext()) .getScaledMinimumFlingVelocity() && Math.abs(mVelocity) < ViewConfiguration .get(getContext()).getScaledMaximumFlingVelocity()) { if (mVelocity > 0) { openDrawer(); } else { closeDrawer(); } } else { if (mLastMotionX > getWidth() / 2) { openDrawer(); } else { closeDrawer(); } } mVelocityTracker.recycle(); return false; } return true; } 

131行是:

  mVelocityTracker.recycle(); 

我不明白为什么会这样。

Solutions Collecting From Web of "VelocityTracker导致Android 4.4崩溃"

回收后,我必须将速度追踪器设置为空,以使其工作更顺畅

正如在这里的文档中提到的,如果你想再次使用它,你必须调用velocityTracker.clear()

使用velocityTracker.recycle()如果不再使用对象 – 释放资源。 当你的活动被破坏的时候这样做。

(这就是@andryr接受的答案的原因,你正在创build一个新的答案)