ViewPager: java.lang.IllegalArgumentException: pointerIndex out of range
Asked Answered
M

1

3

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!

Mathre answered 23/2, 2015 at 9:55 Comment(2)
Did you get an answer here?Superfluous
if you thinking to use LIstView inside ViewPager then already lot of other solution already available. Googleing to this "use list view inside viewpager" will give few option. Check once.Mathre
L
-1

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.

Lama answered 16/7, 2015 at 16:39 Comment(1)
MotionEventCompatEclair just calls MotionEvent and nothing else, so this does not really solves the problem.Naze

© 2022 - 2024 — McMap. All rights reserved.