26

我在 crashlytics 上遇到了一些IllegalArgumentException: pointerIndex out of range崩溃,我不明白发生了什么。它不仅限于一个 android 版本或设备,它发生在各种设备上的 5.0.1、4.4.4、4.4.2、4.0.4、2.3.6 上。以下是更多上下文的完整日志输出。

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)

这是另一个相关的崩溃报告,来自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)

所以我的问题是是什么导致了这个错误,缓解这个问题的可接受方法是什么?

编辑:这是上面崩溃中引用的MotionEvent.java:1998的链接。

编辑:这是我的 onDestroy 的样子:

@Override
public void onDestroy() {

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

    super.onDestroy();
}

具体BaseActivity.java:105是我打电话的地方super.onDestroy();

4

6 回答 6

15

我假设在仍然发生触摸事件时抛出异常(在 Activity 即将发生时流式传输到本机触摸onDestroy()。为了避免崩溃,可以通过捕获异常来缓解这种情况。Activity 即将被销毁如果它已经进入这个状态。

我不确定(尚未测试),但如果捕获异常不适合您,您可以尝试阻止任何事件传递给实现。

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;
    }
}

或者

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

    super.onDestroy();
}

取 2

SwipeRefreshLayout 尝试从无效的指针索引中获取 getY(),如果找不到则调用findPointerIndex(ev, activePointer)返回。-1阻止使用小于 0 的无效指针和大于或等于该事件的指针计数的指针索引分派触摸事件可能会阻止本机 MotionEvent实现中的指针验证引发 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);
    }
}
于 2015-01-14T01:48:15.863 回答
12

它已记录在AOSP 问题跟踪器中。

SwipeRefreshLayout您可以通过子类化并捕获异常来修复它,例如:

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;
        }
    }
}
于 2015-07-16T11:33:38.903 回答
1

如果您在应用程序中看不到任何明显的问题,您可以尝试使用自定义的“静默”版本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;
        }
    }
}
于 2015-01-10T15:19:59.560 回答
1

对于仍在寻找的任何人,当触摸事件与导航抽屉发生冲突时,我看到了这一点。我在我的活动的 onTouch 中添加了一个检查

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

而且似乎更好。在 NavigationDrawerFragment 中:

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

mIsDrawerOpen并返回isDrawerOpen()

于 2015-06-24T05:44:44.083 回答
0

当 ViewPager(扩展 ViewGroup)中的项目计数设置不正确时,我看到了类似的错误。android.view.MotionEvent.getY(10)因此,当列表中有 10 个(不是 11 个)项目时,应用程序会调用。您可以通过获取 ViewGroup 中的内容的计数来尝试调试。

我也很想知道你的 com.mypackage.myapp.BaseActivity.onDestroy 方法里面有什么。

另外,我发现了这一点,Eclair 中显然存在一个错误:https ://stackoverflow.com/a/16519902/2832027 难道 Eclair 中有一个错误已修复,但有些人没有得到更新,如果它在您的 Eclair 版本上运行良好?

更新:查看您的 onDestroy 方法和错误 - 看起来调用 onDestroy 可能会触发 ViewGroup 中的最后一个事件,该事件调用正在查看的组中的最后一个视图。能否AppMsg.cancelAll()SuperCardToast.cancelAllSuperCardToasts()破坏这些观点?您可以在 onDestroy 方法中将 super.onDestroy() 放在它们之前吗?

但是,如果这是问题所在,那么您无法自己复制它是很奇怪的。这也可能是用户 ViewGroup 与您自己的长度的问题。如果你的 ViewGroup 很短,你能复制这个问题吗?

您显示的错误日志似乎仅适用于 Eclair,但您说问题发生在所有设备上。您可以发布非 Eclair 设备的错误日志吗?

于 2015-01-13T10:31:00.510 回答
0

似乎在 SwipeRefreshLayout 的 onTouchEvent 中,他们只是在 ACTION_CANCEL 上添加了 pointerId 验证,但没有添加可用于 ACTION_MOVE 的 pointerIndex 验证。所以我为它创建了一个自定义类并为 ACTION_CANCEL 和 ACTION_UP 处理它。

到目前为止,一切正常,并且很快就会发布我的应用程序的更新。如果仍然在 crashlytics 上遇到任何异常,那么我将更新我的解决方案。直到那个时候享受:)

公共类 SwipeRefreshLayoutX 扩展 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);
    }
}

}

于 2015-03-24T07:46:52.463 回答