java.lang.IllegalArgumentException: invalid pointerIndex -1 for MotionEvent

问题描述

错误 AndroidRuntime: java.lang.IllegalArgumentException: invalid pointerIndex -1 for MotionEvent 表示由于无效的指针索引导致 Android 应用程序崩溃MotionEvent目的。此错误通常发生在应用处理触摸事件时,尤其是使用ViewPager在不同的视图或页面之间滑动。

关于此报错的 Google Issue Tracker : https://issuetracker.google.com/issues/308367897

具体日志

05-24 09:56:28.218584 20502 20502 E AndroidRuntime: FATAL EXCEPTION: main
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: Process: com.xxx.calculator, PID: 20502
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: java.lang.IllegalArgumentException: invalid pointerIndex -1 for MotionEvent { action=MOVE, id[0]=1, x[0]=2252.04, y[0]=985.088, historySize=2, eventTime=1273843185000, downTime=1273305149000, deviceId=4, source=TOUCHSCREEN, displayId=0, eventId=552843928}
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.MotionEvent.nativeGetAxisValue(Native Method)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.MotionEvent.getX(MotionEvent.java:2460)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at androidx.viewpager.widget.ViewPager.onInterceptTouchEvent(Unknown Source:56)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2664)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3178)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2824)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3178)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2824)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3178)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2824)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3178)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2824)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:511)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1899)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.app.Activity.dispatchTouchEvent(Activity.java:4278)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:469)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.View.dispatchPointerEvent(View.java:15320)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:6763)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:6544)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5996)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:6053)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:6019)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:6184)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:6027)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:6241)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6000)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:6053)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:6019)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:6027)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6000)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:9154)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:9105)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:9060)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:9302)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:267)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:247)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:9239)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:9402)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1315)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1323)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.Choreographer.doCallbacks(Choreographer.java:956)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.Choreographer.doFrame(Choreographer.java:851)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1298)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.os.Handler.handleCallback(Handler.java:942)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.os.Handler.dispatchMessage(Handler.java:99)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.os.Looper.loopOnce(Looper.java:201)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.os.Looper.loop(Looper.java:288)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at android.app.ActivityThread.main(ActivityThread.java:7963)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime:  at java.lang.reflect.Method.invoke(Native Method)
05-24 09:56:28.218621 20502 20502 E AndroidRuntime:  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:569)
05-24 09:56:28.218621 20502 20502 E AndroidRuntime:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1034)

Android ViewPager 中的拦截所有触摸屏动作实现 onInterceptTouchEvent

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {/** This method JUST determines whether we want to intercept the motion.* If we return true, onMotionEvent will be called and we do the actual* scrolling there.*/final int action = ev.getAction() & MotionEvent.ACTION_MASK;// Always take care of the touch gesture being complete.if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {// Release the drag.if (DEBUG) Log.v(TAG, "Intercept done!");resetTouch();return false;}// Nothing more to do here if we have decided whether or not we// are dragging.if (action != MotionEvent.ACTION_DOWN) {if (mIsBeingDragged) {if (DEBUG) Log.v(TAG, "Intercept returning true!");return true;}if (mIsUnableToDrag) {if (DEBUG) Log.v(TAG, "Intercept returning false!");return false;}}switch (action) {case MotionEvent.ACTION_MOVE: {/** mIsBeingDragged == false, otherwise the shortcut would have caught it. Check* whether the user has moved far enough from his original down touch.*//** Locally do absolute value. mLastMotionY is set to the y value* of the down event.*/final int activePointerId = mActivePointerId;if (activePointerId == INVALID_POINTER) {// If we don't have a valid id, the touch down wasn't on content.break;}final int pointerIndex = ev.findPointerIndex(activePointerId);final float x = ev.getX(pointerIndex);final float dx = x - mLastMotionX;final float xDiff = Math.abs(dx);final float y = ev.getY(pointerIndex);final float yDiff = Math.abs(y - mInitialMotionY);if (DEBUG) Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);if (dx != 0 && !isGutterDrag(mLastMotionX, dx)&& canScroll(this, false, (int) dx, (int) x, (int) y)) {// Nested view has scrollable area under this point. Let it be handled there.mLastMotionX = x;mLastMotionY = y;mIsUnableToDrag = true;return false;}if (xDiff > mTouchSlop && xDiff * 0.5f > yDiff) {if (DEBUG) Log.v(TAG, "Starting drag!");mIsBeingDragged = true;requestParentDisallowInterceptTouchEvent(true);setScrollState(SCROLL_STATE_DRAGGING);mLastMotionX = dx > 0? mInitialMotionX + mTouchSlop : mInitialMotionX - mTouchSlop;mLastMotionY = y;setScrollingCacheEnabled(true);} else if (yDiff > mTouchSlop) {// The finger has moved enough in the vertical// direction to be counted as a drag...  abort// any attempt to drag horizontally, to work correctly// with children that have scrolling containers.if (DEBUG) Log.v(TAG, "Starting unable to drag!");mIsUnableToDrag = true;}if (mIsBeingDragged) {// Scroll to follow the motion eventif (performDrag(x)) {ViewCompat.postInvalidateOnAnimation(this);}}break;}case MotionEvent.ACTION_DOWN: {/** Remember location of down touch.* ACTION_DOWN always refers to pointer index 0.*/mLastMotionX = mInitialMotionX = ev.getX();mLastMotionY = mInitialMotionY = ev.getY();mActivePointerId = ev.getPointerId(0);mIsUnableToDrag = false;mIsScrollStarted = true;mScroller.computeScrollOffset();if (mScrollState == SCROLL_STATE_SETTLING&& Math.abs(mScroller.getFinalX() - mScroller.getCurrX()) > mCloseEnough) {// Let the user 'catch' the pager as it animates.mScroller.abortAnimation();mPopulatePending = false;populate();mIsBeingDragged = true;requestParentDisallowInterceptTouchEvent(true);setScrollState(SCROLL_STATE_DRAGGING);} else {completeScroll(false);mIsBeingDragged = false;}if (DEBUG) {Log.v(TAG, "Down at " + mLastMotionX + "," + mLastMotionY+ " mIsBeingDragged=" + mIsBeingDragged+ "mIsUnableToDrag=" + mIsUnableToDrag);}break;}case MotionEvent.ACTION_POINTER_UP:onSecondaryPointerUp(ev);break;}if (mVelocityTracker == null) {mVelocityTracker = VelocityTracker.obtain();}mVelocityTracker.addMovement(ev);/** The only time we want to intercept motion events is if we are in the* drag mode.*/return mIsBeingDragged;
}

根据Log信息定位报错的问题点

final float x = ev.getX(pointerIndex);

修改方案

捕获IllegalArgumentException(非法参数异常)异常

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;import androidx.viewpager.widget.ViewPager;public class FixedViewPager extends ViewPager {public FixedViewPager(Context context) {super(context);}public FixedViewPager(Context context, AttributeSet attrs) {super(context, attrs);}@Overridepublic boolean onTouchEvent(MotionEvent ev) {try {return super.onTouchEvent(ev);} catch (Exception ex) {ex.printStackTrace();}return false;}@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {try {return super.onInterceptTouchEvent(ev);} catch (Exception ex) {ex.printStackTrace();}return false;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/845018.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Spring EL<一>✈️ 】SL 表达式的应用

目录 🍸前言 🍻一、Spring EL 1.1 定义 1.2 常见使用方式 🍺二、项目案例 2.1 实现一个简单的案例 2.2 创建注解 2.3 切面类实现 2.4 创建测试接口 2.5 测试 🍹三、章末 🍸前言 小伙伴们大家好,前段时间…

32. 【Java教程】集合

在前面的小节中,我们学习了数组,本小节学习的集合同样用于存放一组数据,我们将学习什么是集合、集合的应用场景 ,在应用场景部分我们将对比 Java 数组与集合的区别,还将系统介绍 Java 集合的架构,也将结合实…

【观察】数字化生存时代已来临,能源转型如何实现“再升级”?

20多年前,尼古拉斯尼葛洛庞帝在《数字化生存》一书中预言:“数字化生存是现代社会中以新技术为基础的新的生存方式。” 随着数字经济的蓬勃发展,尼葛洛庞帝的预言逐渐被验证。今天,新技术带来的数字化和智能化正全方位影响着经济…

【赠书第27期】向AI提问的艺术:提示工程入门与应用

文章目录 前言 1 问题的构建 1.1 明确性与具体性 1.2 结构化与层次性 1.3 相关性与针对性 2 提问的技巧 2.1 简洁明了 2.2 避免歧义 2.3 使用自然语言 3 与AI的互动策略 3.1 耐心与理解 3.2 逐步引导 3.3 反馈与调整 4 总结与展望 5 推荐图书 6 粉丝福利 前言 …

定时器与PWM的LED控制

目录 一、基础概念定时器定时器类型定时器特性 PWM定义占空比原理 二、实验1.LED周期性亮灭定时器TIM2配置GPIO引脚设置工程相关参数配置Keil编写程序 2.LED呼吸灯(PWM)呼吸灯原理Keil编写程序Keil虚拟示波器,观察 PWM输出波形设置点击setup,并设置观察引…

Sapphire开发日志(三)

任务描述 本周实现并封装了模型的运行,需要在前端安装依赖的库,编写对应的处理函数。同时需要能够展示出抠图效果。 依赖与环境配置 尝试在前端接入并运行onnx。 在前端运行onnx依赖于onnxruntime-web这个包,这个包可以在浏览器进行模型推…

antV+vue3 单选框选中加入逻辑运算,再次点击取消选中,不计入逻辑运算

文章目录 antVvue3 单选框选中加入逻辑运算,再次点击取消选中,不计入逻辑运算需求增加点击事件逻辑处理效果 antVvue3 单选框选中加入逻辑运算,再次点击取消选中,不计入逻辑运算 需求 在做项目时,多次遇到单选框需要再…

【2024新版】银系统源码/超市收银系统/智慧新零售/ERP进销存管理/线上商城/商户助手

>>>系统简述:本系统适用于超吃便利店,美妆母婴行业,服装鞋帽行业,食品零售行业,3C数码电子行业,食品生鲜等一切零售行业,产品功能角色介绍如下 合伙人:无限发展代理商和商…

Pod 控制器

前言 Pod 是 Kubernetes 集群中能够被创建和管理的最小部署单元。所以需要有工具去操作和管理它们的生命周期,这里就需要用到控制器了。 Pod 控制器由 master 的 kube-controller-manager 组件提供,常见的此类控制器有 Replication Controller、ReplicaSet、Deploym…

React-生成随机数和日期格式化

生成随机数 uuid文档:https://github.com/uuidjs/uuid npm install uuid import {v4 as uuidV4} from uuid 使用: uuidV4() 日期格式化 dayjs文档:安装 | Day.js中文网 npm install dayjs import dayjs from dayjs

算法-扫描线

目录 什么是扫描线算法? 扫描线简单应用 更多的扫描线 什么是扫描线算法? 在计算几何中,扫描线算法(scan line algorithm)一般用来解决几何图形的面积交并,周长交并问题,扫描线算法的核心思想…

SAPUI5基础知识3 - 引导过程(Bootstrap)

1. 背景 在上一篇博客中,我们已经建立出了第一个SAPUI5项目,接下来,我们将为这个项目添加引导过程。 在动手练习之前,让我们先解释一下什么引导过程。 1.1 什么是引导过程? 在计算机科学中,引导过程也称…

Spring——依赖项

文章目录 依赖注入基于构造函数的依赖注入基于 Setter 的依赖注入依赖解析过程依赖注入的示例 依赖关系和配置详细信息直接值(原语、字符串等)idref标签References to Other Beans (对其他 Bean的引用)Inner Beans(内部…

5、css3 自动动画渐变背景

效果例图&#xff1a;&#xff08;因gif图片太大&#xff0c;而csdn只能上传小于5m图片&#xff0c;所以无法上传&#xff09; 1、首先上传html代码&#xff1a; <!DOCTYPE html> <html lang"zh"> <head><meta charset"UTF-8">&l…

【鸟哥】Linux笔记-硬件搭配

在Linux这个系统当中&#xff0c;几乎所有的硬件设备文件都在/dev这个目录内。打印机与软盘呢&#xff1f;分别是/dev/lp0, /dev/fd0。 几个常见的设备与其在Linux当中的文件名&#xff1a; 如果你的机器使用的是跟网际网络供应商 &#xff08;ISP&#xff09; 申请使用的云端…

C++标准模板(STL)- C 内存管理库 - 扩张之前分配的内存块 (std::realloc)

C 内存管理库 扩张之前分配的内存块 std::realloc void* realloc( void* ptr, std::size_t new_size ); 重分配给定的内存区域。它必须是 std::malloc() 、 std::calloc() 或 std::realloc() 先前分配的&#xff0c;且仍未被 std::free() 释放&#xff0c;否则行为未定义。…

二叉树的构建和遍历(oj题)

一、题目链接 https://www.nowcoder.com/practice/4b91205483694f449f94c179883c1fef?tpId60&&tqId29483&rp1&ru/activity/oj&qru/ta/tsing-kaoyan/question-ranking 二、题目思路 利用先序遍历的方法&#xff0c;构建二叉树。为了保证在递归中&#xf…

USB主机模式——Android

理论 摘自&#xff1a;USB 主机和配件概览 | Connectivity | Android Developers (google.cn) Android 通过 USB 配件和 USB 主机两种模式支持各种 USB 外围设备和 Android USB 配件&#xff08;实现 Android 配件协议的硬件&#xff09;。 在 USB 主机模式下&#xff0…

Redis 中的 Zset 数据结构详解

目录 用法 1. 增 2. 删 3. 查 4. 交&#xff0c;并 编码方式 应用场景 Redis 中的 Zset&#xff08;有序集合&#xff09;是一种将元素按照分数进行排序的数据结构。与上篇写的SetRedis 中的 Set 数据结构详解不同&#xff0c;Zset 中的每个元素都关联一个浮点数类型的…

Jmeter的线程组之间传递参数

使用jemter做接口测试&#xff0c;有时候需要会遇到不同线程组之间调用相同变量的情况&#xff0c;最多见的就是token的传递&#xff0c;网上有很多处理方法&#xff0c;这里只记录setProperty的办法&#xff0c;一招鲜走遍天&#xff01; 首先我有两个线程组&#xff1a; 线程…