2

在此处输入图像描述我正在尝试在 ListView 项目上滑动删除

onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY),我在 MotionEvent e1 上获得 null

请帮助我,提前谢谢

@Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        Log.e(THIS_FILE, "======onFling "+REL_SWIPE_MAX_OFF_PATH+" , "+e1+" , "+e2);
        if (Math.abs(e1.getY() - e2.getY()) > REL_SWIPE_MAX_OFF_PATH)
            return false;
        if (e1.getX() - e2.getX() > REL_SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > REL_SWIPE_THRESHOLD_VELOCITY) {

            int pos = getListView()
                    .pointToPosition((int) e1.getX(), (int) e2.getY());

            if (pos >= 0 && temp_position == pos)
                getSwipeItem(false, pos);
        } else if (e2.getX() - e1.getX() > REL_SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > REL_SWIPE_THRESHOLD_VELOCITY) {

            int pos = getListView()
                    .pointToPosition((int) e1.getX(), (int) e2.getY());
            if (pos >= 0 && temp_position == pos)
                getSwipeItem(true, pos);

        }
        return false;
    }

这是我的日志

09-05 14:41:45.086: E/DialerFragment(31827): ======onFling 333 , null , MotionEvent {     action=ACTION_UP, id[0]=0, x[0]=390.39392, y[0]=154.0, toolType[0]=TOOL_TYPE_FINGER,     buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0,     eventTime=197101507, downTime=197101342, deviceId=2, source=0x1002 }
09-05 14:41:45.086: E/InputEventReceiver(31827): Exception dispatching input event.
09-05 14:41:45.086: E/MessageQueue-JNI(31827): Exception in MessageQueue callback: handleReceiveCallback
09-05 14:41:45.116: E/MessageQueue-JNI(31827): java.lang.NullPointerException
09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at net.telivo.fiestacancun.ui.tablet.DialerFragment$MyGestureDetector.onFling(DialerFragment.java:    1660)
09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at     android.view.GestureDetector.onTouchEvent(GestureDetector.java:609)
      09-05 14:41:45.116: E/MessageQueue-JNI(31827):    at net.telivo.fiestacancun.ui.tablet.DialerFragment$12.onTouch(DialerFragment.java:967)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.View.dispatchTouchEvent(View.java:7379)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2203)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1938)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1952)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1952)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1952)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1952)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at     android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1952)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1952)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1952)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at     android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1952)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1952)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at    android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1952)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1952)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1966)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1418)
09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.app.Activity.dispatchTouchEvent(Activity.java:2424)
09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1914)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.View.dispatchPointerEvent(View.java:7564)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3883)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3778)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at     android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at     android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5419)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at     android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5399)
09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5370)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5493)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:182)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.os.MessageQueue.nativePollOnce(Native Method)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.os.MessageQueue.next(MessageQueue.java:132)
    09-05 14:41:45.116: E/MessageQueue-JNI(31827):  at android.os.Looper.l
    09-05 14:41:45.116: D/AndroidRuntime(31827): Shutting down VM
    09-05 14:41:45.116: W/dalvikvm(31827): threadid=1: thread exiting with uncaught exception (group=0x4184c700)
4

2 回答 2

0

如果你想使用滑动删除功能,你为什么不直接使用这样的预制库https://github.com/romannurik/Android-SwipeToDismiss和这个https://github.com/timroes/SwipeToDismissUndoList

于 2013-09-05T12:28:33.590 回答
-1

您的日志显示 e1 为 null 并且historySize=0,因此它只有一个要报告的事件。您需要添加一个空检查以确保您拥有这两个事件。

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    Log.e(THIS_FILE, "======onFling "+REL_SWIPE_MAX_OFF_PATH+" , "+e1+" , "+e2);
    if (e1 == null || e2 == null) {
        return false;  // If it still gives you trouble return true
    }

    if (Math.abs(e1.getY() - e2.getY()) > REL_SWIPE_MAX_OFF_PATH)
        return false;
    if (e1.getX() - e2.getX() > REL_SWIPE_MIN_DISTANCE
            && Math.abs(velocityX) > REL_SWIPE_THRESHOLD_VELOCITY) {

        int pos = getListView()
                .pointToPosition((int) e1.getX(), (int) e2.getY());

        if (pos >= 0 && temp_position == pos)
            getSwipeItem(false, pos);
    } else if (e2.getX() - e1.getX() > REL_SWIPE_MIN_DISTANCE
            && Math.abs(velocityX) > REL_SWIPE_THRESHOLD_VELOCITY) {

        int pos = getListView()
                .pointToPosition((int) e1.getX(), (int) e2.getY());
        if (pos >= 0 && temp_position == pos)
            getSwipeItem(true, pos);

    }
    return false;
}
于 2013-09-05T10:06:15.533 回答