ViewPager:java.lang.IllegalArgumentException:pointerIndex 超出范围 [英] ViewPager: java.lang.IllegalArgumentException: pointerIndex out of range

查看:25
本文介绍了ViewPager:java.lang.IllegalArgumentException:pointerIndex 超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个代码来根据建议 这里.我使用 ViewPager 作为 ViewGroup 和 ListView(我知道 #thisbad)作为 Fragment 的子视图.

这就是我想要实现的目标:

  1. 在子视图上检测多点触控事件
  2. 然后将触摸控制传递给父母

但是在将侦听器事件从子视图传递到父视图时,它会给出以下错误:

E/AndroidRuntime(11414): java.lang.IllegalArgumentException:pointerIndex 超出范围E/AndroidRuntime(11414):在 android.view.MotionEvent.nativeGetAxisValue(Native Method)E/AndroidRuntime(11414):在 android.view.MotionEvent.getX(MotionEvent.java:1979)E/AndroidRuntime(11414):在 android.support.v4.view.MotionEventCompatEclair.getX(MotionEventCompatEclair.java:32)E/AndroidRuntime(11414):在 android.support.v4.view.MotionEventCompat$EclairMotionEventVersionImpl.getX(MotionEventCompat.java:91)E/AndroidRuntime(11414):在 android.support.v4.view.MotionEventCompat.getX(MotionEventCompat.java:219)E/AndroidRuntime(11414): 在 android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:1971) .....E/AndroidRuntime(11414):在 android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:5588)E/AndroidRuntime(11414):在 android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:5634)E/AndroidRuntime(11414):在 android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)E/AndroidRuntime(11414):在 android.view.Choreographer.doCallbacks(Choreographer.java:574)E/AndroidRuntime(11414):在 android.view.Choreographer.doFrame(Choreographer.java:542)

我检查了其他一些帖子有同样的问题 like这里 但他们都在使用指针索引来做一些事情,但在我的情况下,我现在只是将控制权传递给父(ViewPager)视图.p>

分析:这里我检查了使用FrameLayout而不是子片段中的Listview.并且它没有任何提到的问题..但不是 ListView.

设备信息: OS V4.4.4 S5.

任何建议!

解决方案

不知道为什么 MotionEventCompat 会调用 MotionEventCompatEclair,我在代码中看到,有一个 MotionEventCompatHoneycomb 重载,但是我在 Moto XT1040 上遇到了同样的问题在安卓 4.4.4 中.

我的解决方案是创建我的 ViewPager 版本(我只是从 android 源代码复制了整个类)并在方法 onInterceptTouchEvent(MotionEvent ev) 的情况下 MotionEvent.ACTION_MOVE 更改初始行:

final int activePointerId = mActivePointerId;if (activePointerId == INVALID_POINTER) {//如果我们没有有效的 id,则触地不在内容上.休息;}final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);最终浮点 x = MotionEventCompat.getX(ev, pointerIndex);最终浮点 dx = x - mLastMotionX;最终浮点 xDiff = Math.abs(dx);最终浮点 y = MotionEventCompat.getY(ev, pointerIndex);最终浮点 yDiff = Math.abs(y - mInitialMotionY);

到:

final int activePointerId = mActivePointerId;if (activePointerId == INVALID_POINTER) {//如果我们没有有效的 id,则触地不在内容上.休息;}final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);最终浮点 x = ev.getX(pointerIndex);最终浮点 dx = x - mLastMotionX;最终浮点 xDiff = Math.abs(dx);最终浮点 y = ev.getY(pointerIndex);最终浮点 yDiff = Math.abs(y - mInitialMotionY);

唯一的变化是x和y变量,我改为直接调用MotionEvent getX和getY方法,忽略MotionEventCompat.

我的应用程序最低 api 14,如果您支持 14 之前的内容,我建议您从您的版本中调用最接近的 MotionEventCompat.您只需要阻止调用 MotivoEventCompatEclair 版本即可.

I am writing an code to handle touch event based on what suggested here. I am using ViewPager as an ViewGroup and ListView(I know #thisbad) as child view of Fragment.

This is what I wanted to achieve:

  1. Detect multitouch event on child view
  2. then pass touch control to parent

but While passing listener event from child View to Parent view it giving following error:

E/AndroidRuntime(11414): java.lang.IllegalArgumentException:pointerIndex out of range      
E/AndroidRuntime(11414):    at android.view.MotionEvent.nativeGetAxisValue(Native Method)
E/AndroidRuntime(11414):    at android.view.MotionEvent.getX(MotionEvent.java:1979)
E/AndroidRuntime(11414):    at android.support.v4.view.MotionEventCompatEclair.getX(MotionEventCompatEclair.java:32)
E/AndroidRuntime(11414):    at android.support.v4.view.MotionEventCompat$EclairMotionEventVersionImpl.getX(MotionEventCompat.java:91)
E/AndroidRuntime(11414):    at android.support.v4.view.MotionEventCompat.getX(MotionEventCompat.java:219)
E/AndroidRuntime(11414):    at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:1971) ..................
E/AndroidRuntime(11414):    at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:5588)
E/AndroidRuntime(11414):    at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:5634)
E/AndroidRuntime(11414):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
E/AndroidRuntime(11414):    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
E/AndroidRuntime(11414):    at android.view.Choreographer.doFrame(Choreographer.java:542)

I checked with some other post have same issue like and here but all they are using Pointer indexing to do somehting but in my case I am just passing control to parent(ViewPager) view now.

Analysis: Here I checked to use FrameLayout instead to Listview in Child Fragment. and it's working without any mentioned issue..but not with ListView.

Device Info: OS V4.4.4 S5.

Any suggestion!

解决方案

I don't know why MotionEventCompat is calling MotionEventCompatEclair, as I saw in the code, there are a MotionEventCompatHoneycomb overload, but I had the same problem with Moto XT1040 in android 4.4.4.

The solution for me was create my version of ViewPager(I just copied the entire class from android source) and in the method onInterceptTouchEvent(MotionEvent ev) in the case of MotionEvent.ACTION_MOVE change the initial lines from:

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 = MotionEventCompat.findPointerIndex(ev, activePointerId);
final float x = MotionEventCompat.getX(ev, pointerIndex);
final float dx = x - mLastMotionX;
final float xDiff = Math.abs(dx);
final float y = MotionEventCompat.getY(ev, pointerIndex);
final float yDiff = Math.abs(y - mInitialMotionY);

to:

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 = MotionEventCompat.findPointerIndex(ev, 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);

The only change was in the x and y variables, I changed to call the MotionEvent getX and getY method directly, ignoring the MotionEventCompat.

My app was minimum api 14, if you are supporting something previous from 14, I suggest to you call the closest MotionEventCompat from your version. You just need to prevent from calling the MotivoEventCompatEclair version.

这篇关于ViewPager:java.lang.IllegalArgumentException:pointerIndex 超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆