gpt4 book ai didi

android - IllegalArgumentException : pointerIndex out of range from SwipeRefreshLayout

转载 作者:IT老高 更新时间:2023-10-28 22:14:39 27 4
gpt4 key购买 nike

我在 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(); 的地方。

最佳答案

我假设在仍然发生触摸事件时引发异常(当 Activity 即将 onDestroy() 时向下流到 native 触摸。可以通过捕获异常来缓解这种情况为了避免crash,进入这个状态的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 的无效指针和大于或等于该事件的指针计数的指针索引分派(dispatch)触摸事件可能会阻止验证 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);
}
}

关于android - IllegalArgumentException : pointerIndex out of range from SwipeRefreshLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27662682/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com