我的应用中有一个自定义的抽屉布局实现。在Android 2.3和4.0.4上运行良好,但在Android 4.4上几乎每次打开或关闭抽屉时都会崩溃。以下是错误日志:
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();
我不明白为什么会发生这种情况。