我在崩溃问题上遇到了一些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();
的地方。
答案 0 :(得分:13)
我假设当触摸事件仍然发生时抛出异常(当活动即将onDestroy()
时流式传输到本机触摸。可以通过捕获异常来避免这种情况,以避免如果活动进入这种状态,活动即将被销毁。
我不确定(没有经过测试),但如果捕获异常不适合您,您可以尝试阻止任何事件传递给实现。
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的无效指针调度触摸事件并且指针索引大于或等于该事件的指针计数可能会阻止验证native 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);
}
}
答案 1 :(得分:10)
已在AOSP issue tracker登录。
您可以通过子类化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;
}
}
}
答案 2 :(得分:1)
如果您在应用中看不到任何明显问题,可以尝试使用自定义&#34;无声&#34;版本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;
}
}
}
答案 3 :(得分: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()
答案 4 :(得分:0)
当ViewPager(扩展ViewGroup)中的项目计数设置不正确时,我发现了类似的错误。因此,当列表中有10个(不是11个)项目时,应用程序将调用android.view.MotionEvent.getY(10)
。您可以通过获取ViewGroup中的内容来尝试调试。
我也有兴趣知道com.mypackage.myapp.BaseActivity.onDestroy方法中的内容。
另外,我发现了这一点,Eclair显然存在一个错误:https://stackoverflow.com/a/16519902/2832027 难道Eclair中有一个错误已经修复但有些人没有得到更新,如果它在您的Eclair版本上运行正常吗?
UPDATE:查看onDestroy方法和错误 - 看起来调用onDestroy可能会触发ViewGroup中的最后一个事件,该事件调用正在查看的组中的最后一个视图。 AppMsg.cancelAll()
或SuperCardToast.cancelAllSuperCardToasts()
会破坏这些观点吗?你可以在你的onDestroy方法中放置super.onDestroy()吗?
但是,如果这是问题所在,很奇怪你自己无法复制它。这也可能是用户ViewGroup与您自己的长度相关的问题。如果你有非常短的ViewGroups,你可以复制这个问题吗?
您显示的错误日志似乎只适用于Eclair,但您说所有设备上都会出现此问题。你能发布非Eclair设备的错误日志吗?
答案 5 :(得分:0)
似乎在SwipeRefreshLayout的onTouchEvent中,他们刚刚在ACTION_CANCEL上添加了pointerId验证,但没有添加可用于ACTION_MOVE的pointerIndex验证。所以我为它创建了一个自定义类,并为ACTION_CANCEL&amp;处理它。 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);
}
}
}