MrEngineer13
MrEngineer13

Reputation: 38876

IllegalArgumentException: pointerIndex out of range from SwipeRefreshLayout

I've been getting some of these IllegalArgumentException: pointerIndex out of range crashes on crashlytics and I don't understand what's happening. It's not limited to one android build or device, it happens on 5.0.1, 4.4.4, 4.4.2, 4.0.4, 2.3.6 all on various devices. Below is the full log output for more context.

java.lang.RuntimeException: Unable to destroy activity {com.mypackage.myapp/com.mypackage.myapp.MyListActivity}: java.lang.IllegalArgumentException: pointerIndex out of range
       at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3671)
       at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3689)
       at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3889)
       at android.app.ActivityThread.access$900(ActivityThread.java:144)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:135)
       at android.app.ActivityThread.main(ActivityThread.java:5221)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
Caused by: java.lang.IllegalArgumentException: pointerIndex out of range
       at android.view.MotionEvent.nativeGetAxisValue(MotionEvent.java)
       at android.view.MotionEvent.getY(MotionEvent.java:1998)
       at android.support.v4.view.MotionEventCompatEclair.getY(MotionEventCompatEclair.java:35)
       at android.support.v4.view.MotionEventCompat$EclairMotionEventVersionImpl.getY(MotionEventCompat.java:95)
       at android.support.v4.view.MotionEventCompat.getY(MotionEventCompat.java:228)
       at android.support.v4.widget.SwipeRefreshLayout.onTouchEvent(SwipeRefreshLayout.java:772)
       at android.view.View.dispatchTouchEvent(View.java:8388)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2398)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2158)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2400)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
       at android.view.ViewGroup.cancelTouchTarget(ViewGroup.java:2340)
       at android.view.ViewGroup.removeViewInternal(ViewGroup.java:4156)
       at android.view.ViewGroup.removeViewInternal(ViewGroup.java:4136)
       at android.view.ViewGroup.removeView(ViewGroup.java:4068)
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1045)
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
       at android.support.v4.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1954)
       at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:313)
       at android.support.v7.app.ActionBarActivity.onDestroy(ActionBarActivity.java:169)
       at com.mypackage.myapp.BaseActivity.onDestroy(BaseActivity.java:105)
       at android.app.Activity.performDestroy(Activity.java:6112)
       at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1140)
       at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3658)
       at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3689)
       at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3889)
       at android.app.ActivityThread.access$900(ActivityThread.java:144)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:135)
       at android.app.ActivityThread.main(ActivityThread.java:5221)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)

Here is another related crash report coming from android.view.MotionEvent.getY().

java.lang.RuntimeException: Unable to destroy activity {com.mypackage.myapp/com.mypackage.myapp.MyListActivity}: java.lang.ArrayIndexOutOfBoundsException
       at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2683)
       at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:2701)
       at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2817)
       at android.app.ActivityThread.access$1600(ActivityThread.java:117)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:946)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:130)
       at android.app.ActivityThread.main(ActivityThread.java:3733)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:507)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:931)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:689)
       at dalvik.system.NativeStart.main(NativeStart.java)
Caused by: java.lang.ArrayIndexOutOfBoundsException
       at android.view.MotionEvent.getY(MotionEvent.java:903)
       at android.support.v4.view.MotionEventCompatEclair.d(MotionEventCompatEclair.java:35)
       at android.support.v4.view.MotionEventCompat$EclairMotionEventVersionImpl.d(MotionEventCompat.java:95)
       at android.support.v4.view.MotionEventCompat.d(MotionEventCompat.java:228)
       at android.support.v4.widget.SwipeRefreshLayout.onTouchEvent(SwipeRefreshLayout.java:772)
       at android.view.View.dispatchTouchEvent(View.java:3971)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
       at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1154)
       at android.view.ViewGroup.removeViewInternal(ViewGroup.java:2201)
       at android.view.ViewGroup.removeViewInternal(ViewGroup.java:2187)
       at android.view.ViewGroup.removeView(ViewGroup.java:2135)
       at android.support.v4.app.FragmentManagerImpl.a(FragmentManager.java:1045)
       at android.support.v4.app.FragmentManagerImpl.a(FragmentManager.java:1126)
       at android.support.v4.app.FragmentManagerImpl.a(FragmentManager.java:1108)
       at android.support.v4.app.FragmentManagerImpl.t(FragmentManager.java:1954)
       at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:313)
       at android.support.v7.app.ActionBarActivity.onDestroy(ActionBarActivity.java:169)
       at com.mypackage.myapp.BaseActivity.onDestroy(BaseActivity.java:105)
       at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2670)
       at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:2701)
       at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2817)
       at android.app.ActivityThread.access$1600(ActivityThread.java:117)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:946)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:130)
       at android.app.ActivityThread.main(ActivityThread.java:3733)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:507)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:931)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:689)
       at dalvik.system.NativeStart.main(NativeStart.java)

So my question is what is causing this error and what would be the acceptable method for mitigating this issue?

EDIT: Here is the link to MotionEvent.java:1998 which is referenced in the crash above.

EDIT: Here is my onDestroy looks like:

@Override
public void onDestroy() {

    AppMsg.cancelAll();
    SuperCardToast.cancelAllSuperCardToasts();

    super.onDestroy();
}

Specifically BaseActivity.java:105 is where I call super.onDestroy();.

Upvotes: 26

Views: 18225

Answers (6)

Sameer Zinzuwadia
Sameer Zinzuwadia

Reputation: 3285

It is already logged at AOSP issue tracker.

You can fix it by subclasing the SwipeRefreshLayout and catching the exception, like:

public class SwipeRefreshLayout extends android.support.v4.widget.SwipeRefreshLayout {
    public SwipeRefreshLayout(Context context) {
        super(context);
    }

    public SwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        try {
            return super.onTouchEvent(ev);
        } catch (IllegalArgumentException e) {
            //Fix for support lib bug, happening when onDestroy() is 
            return true;
        }
    }
}

Upvotes: 12

Sreedevi J
Sreedevi J

Reputation: 673

For anyone still looking, I was seeing this when the touch events were conflicting with the navigation drawer. I added a check in the onTouch of my activity

if (mNavigationDrawerFragment != null && mNavigationDrawerFragment.isDrawerOpen()) {
    return super.onTouchEvent(event);
}

And it seems to be better. in NavigationDrawerFragment:

@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
    super.onDrawerSlide(drawerView, slideOffset);
    mIsDrawerOpen = slideOffset != 0;
    getActivity().invalidateOptionsMenu();
}

and return mIsDrawerOpen in isDrawerOpen()

Upvotes: 1

EEJ
EEJ

Reputation: 678

It seems like inside the onTouchEvent of SwipeRefreshLayout they just added the pointerId validation on ACTION_CANCEL but not added the pointerIndex validation which is available for ACTION_MOVE . So i have created a custom class for it and handle it for ACTION_CANCEL & ACTION_UP.

So far everything is working properly and going to release an update of my app soon. If still get any exception on crashlytics then i will update my solution. Till that time Enjoy :)

public class SwipeRefreshLayoutX extends SwipeRefreshLayout {

private int mActivePointerId;

public SwipeRefreshLayoutX(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

public SwipeRefreshLayoutX(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

@Override
public boolean onTouchEvent(MotionEvent event) {

    final int action = MotionEventCompat.getActionMasked(event);

    switch (action) {

        case MotionEvent.ACTION_DOWN:
            mActivePointerId = MotionEventCompat.getPointerId(event, 0);
            break;

        case MotionEvent.ACTION_POINTER_DOWN:{
            final int index = MotionEventCompat.getActionIndex(event);
            mActivePointerId = MotionEventCompat.getPointerId(event, index);
            break;  
        }

        case MotionEvent.ACTION_POINTER_UP:{
            onSecondaryPointerUp(event);
            break;
        }

        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:{

            final int pointerIndex = MotionEventCompat.findPointerIndex(event, mActivePointerId);

            if (pointerIndex < 0) {
                Log.e("ash", "Got ACTION_UP event but have an invalid active pointer id.");
                return false;
            }

            break;
        }   

    }

    return super.onTouchEvent(event);

}

private void onSecondaryPointerUp(MotionEvent ev) {
    final int pointerIndex = MotionEventCompat.getActionIndex(ev);
    final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
    if (pointerId == mActivePointerId) {
        // This was our active pointer going up. Choose a new
        // active pointer and adjust accordingly.
        final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
        mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
    }
}

}

Upvotes: 0

TTransmit
TTransmit

Reputation: 3350

I've seen a similar error when the count of items in a ViewPager (which extend ViewGroup) was set incorrectly. So the app would call android.view.MotionEvent.getY(10) when there were 10 (not 11) items in the list. You could try debugging by getting the count of what is in the ViewGroup.

I'd also be interested to know what is inside your com.mypackage.myapp.BaseActivity.onDestroy method.

Also, I spotted this, there is apparently a bug in Eclair: https://stackoverflow.com/a/16519902/2832027 Could it be there was a bug in Eclair that has been fixed but some people haven't got the update, if it's working fine on your version of Eclair?

UPDATE: Looking at your onDestroy method and the errors - It looks like calling onDestroy may trigger one last event in the ViewGroup that calls on the last view in the group that was being viewed. Could AppMsg.cancelAll() or SuperCardToast.cancelAllSuperCardToasts() destroy these views? Could you put the super.onDestroy() before them in your onDestroy method?

Though, if this is the problem, it is strange that you can't replicate it yourself. It could also be an issue with the length of users ViewGroup's vs your own. If you have very short ViewGroups can you replicate the issue?

The error logs you show only seem to be for Eclair yet you say the issue occurs on all devices. Can you post an error log for a non-Eclair device?

Upvotes: 0

Nikola Despotoski
Nikola Despotoski

Reputation: 50578

I assume that exception is thrown while there is touch event still occurring (streamed down to the native touch while the Activity is about to onDestroy(). It is ok to mitigate this by catching the exception for the sake of avoiding crash. Activity is about to be destroyed if it has enter this state.

I'm not sure (havent tested), but you could try to prevent any events to be passed to implementation at all, if catching exception does not fit you.

public class ComeUpWithBetterNameSwipeRefreshLayout extends SwipeRefreshLayout {

    private boolean mAcceptEvents;

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

    public void setAcceptEvents(boolean mAcceptEvents) {
        this.mAcceptEvents = mAcceptEvents;
    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return mAcceptEvents? super.onInterceptTouchEvent(ev) : true;
    }

    public ComeUpWithBetterNameSwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        mAcceptEvents = true;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mAcceptEvents = false;
    }
}

or

@Override
public void onDestroy() {
    mSwipeRefreshLayout.setAcceptEvents(false);
    AppMsg.cancelAll();
    SuperCardToast.cancelAllSuperCardToasts();

    super.onDestroy();
}

Take 2:

SwipeRefreshLayout tries to getY() from invalid pointer index, calling findPointerIndex(ev, activePointer) returns -1 if it fails to find. Preventing dispatching touch event with invalid pointer less than 0 and pointer index greater or equal than pointer count for that event will probably prevent validation of pointer inside native MotionEvent implementation from throwing IAE.

 @Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if(ev.getAction() == MotionEvent.ACTION_CANCEL) {
        int pointerCount = MotionEventCompat.getPointerCount(ev);
        int index = MotionEventCompat.getActionIndex(ev);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        index = MotionEventCompat.findPointerIndex(ev,mActivePointerId);
        if (index > -1 && index < pointerCount) {
            super.onInterceptTouchEvent(ev);
        } else {
            return true;
        }
    }else if(ev.getAction() == MotionEventCompat.ACTION_POINTER_DOWN && super.onInterceptTouchEvent(ev)) {
        final int index = MotionEventCompat.getActionIndex(ev);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        return false;
    }else if(ev.getAction() == MotionEventCompat.ACTION_POINTER_UP && super.onInterceptTouchEvent(ev)){
        onSecondaryPointerUp(ev);
        return false;
    }else if(ev.getAction() == MotionEvent.ACTION_DOWN && super.onInterceptTouchEvent(ev)){
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        return false;
    }
    return super.onInterceptTouchEvent(ev);
}

private void onSecondaryPointerUp(MotionEvent ev) {
    final int pointerIndex = MotionEventCompat.getActionIndex(ev);
    final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
    if (pointerId == mActivePointerId) {
        final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
        mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
    }
}

Upvotes: 15

bonnyz
bonnyz

Reputation: 13558

If you don't see any apparent problem in your app, you can try to use a custom "silent" version of the SwipeRefreshLayout:

public class CustomSwipeRefreshLayout extends SwipeRefreshLayout{

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

    public CustomSwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        try{
            return super.onTouchEvent(event);
        }
        catch(Exception e){
            return true;
        }
    }
}

Upvotes: 1

Related Questions