vcalvello / SwipeToAction

*** WARNING: This library is no longer maintained *** An easy way to add a simple 'swipe-and-do-something' behavior to your `RecyclerView` items. Just like in Gmail or Inbox apps.
219 stars 45 forks source link

pointerIndex out of Range error #5

Open Davit-Al opened 8 years ago

Davit-Al commented 8 years ago

Sometimes I get this error when I am scrolliing. Here is stacktrace:

java.lang.IllegalArgumentException: pointerIndex out of range at android.view.MotionEvent.nativeGetAxisValue(Native Method) at android.view.MotionEvent.getX(MotionEvent.java:2069) at co.dift.ui.SwipeToAction$1.onTouch(SwipeToAction.java:91) at android.view.View.dispatchTouchEvent(View.java:7784) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2210) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)

daniil-pastuhov commented 8 years ago

I have the same error. Just to drag left-right is enough. device 4.2.2

hadidez commented 7 years ago

same problem ...

riki-dexter commented 6 years ago

+1. Same problem

imlk0 commented 6 years ago

I found this page: https://blog.csdn.net/nnmmbb/article/details/28419779 It may be a bug of Android system.

I have read the source code of SwipeToAction, and find it set an onTouchListener

...
private void init() {
        recyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent ev) {
                switch (ev.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN: {
                        // http://android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html
                        activePointerId = ev.getPointerId(0);
...
...
                    case MotionEvent.ACTION_MOVE: {
                        final int pointerIndex = ev.findPointerIndex(activePointerId);
                        final float x = ev.getX(pointerIndex);// crash here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                        final float dx = x - downX;
...

It doesn't check the validity of the pointerIndex, so it crashed. I don't want to edit the source code, I avoid it by extends the RecycleView and override the setOnTouchListener method, in the setOnTouchListener method, I construct a static proxy of the true OnTouchListener created by SwipeToAction. and warp the true onTouch method with try{...}catch block, which catched the Exception.

my code:


public class FixExceptionRecycleView extends RecyclerView {
    private static final String LOG_TAG = "FixExceptionRecycleView";

    public FixExceptionRecycleView(Context context) {
        super(context);
    }

    public FixExceptionRecycleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public FixExceptionRecycleView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void setOnTouchListener(OnTouchListener l) {
        super.setOnTouchListener(new FixExceptionOnTouchListener(l));
    }

    static class FixExceptionOnTouchListener implements OnTouchListener {

        private final OnTouchListener proxyedListener;

        public FixExceptionOnTouchListener(OnTouchListener proxyedListener) {
            this.proxyedListener = proxyedListener;
        }

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            try {
                return this.proxyedListener.onTouch(v, event);
            } catch (IllegalArgumentException e) {
                Log.i(LOG_TAG, "IllegalArgumentException catched", e);
            }
            return false;
        }
    }
}

@riki-dexter @hadidez @daniil-pastuhov @DatoAlxanishvili