VelocityTracker导致Android 4.4崩溃

时间:2014-09-27 12:56:15

标签: android crash android-4.4-kitkat

在我的应用程序中,我有一个抽屉布局的自定义实现。它适用于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();

我不明白为什么会这样。

2 个答案:

答案 0 :(得分:25)

我必须在回收它之后将我的velocitytracker设置为null以使其正常工作

答案 1 :(得分:3)

正如文档here中所提到的,如果您想再次使用它,则必须致电velocityTracker.clear()

如果您不再使用该对象,请使用velocityTracker.recycle()来释放资源。在您的活动被销毁时执行此操作。

(这就是@andryr接受的答案有效的原因。你正在创建一个新答案)