Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.IllegalStateException: Page can only be offset by a positive amount

I am getting this error in my Pre-Launch Reports, but it doesn't show any references to my .java files. Can anyone please shed some light on this?

I'm using a ViewPager2 (1.0.0) with page transformations.

Issue: java.lang.IllegalStateException: Page can only be offset by a positive amount, not by -54
 FATAL EXCEPTION: main
Process: [redacted], PID: 18424
java.lang.IllegalStateException: Page can only be offset by a positive amount, not by -54
    at androidx.viewpager2.widget.ScrollEventAdapter.updateScrollEventValues(ScrollEventAdapter.java:280)
    at androidx.viewpager2.widget.ScrollEventAdapter.onScrolled(ScrollEventAdapter.java:178)
    at androidx.recyclerview.widget.RecyclerView.dispatchOnScrolled(RecyclerView.java:5173)
    at androidx.recyclerview.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:5338)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
    at android.view.Choreographer.doCallbacks(Choreographer.java:670)
    at android.view.Choreographer.doFrame(Choreographer.java:603)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
    at android.os.Handler.handleCallback(Handler.java:746)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5459)
    at java.lang.reflect.Method.invoke(Method.java)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

I found this page about ViewPager2 with reference to a similar error being a bug of ViewPager2.

Possibly relevant:

I only started getting this error very recently. I recently changed my code in the following manner. I'm overriding the getItemCount() method, and the NUM_PAGES value that it returns varies depending on a selectable option elsewhere. Previously I would just recreate() the Activity holding all the Fragments when this option is toggled. Instead, now I call Objects.requireNonNull(recyclerViewAdapter).notifyDataSetChanged(); and it works nicely. I'm not sure if this is the correct way to add/remove or show/hide the rightmost fragment, as other options leaves an empty page where the fragment normally would be.

EDIT: I removed my PageTransformer from the ViewPager2 - which only sets the alpha - and the error is currently gone. But I think it may be co-incidence. I haven't seen the error 'in the wild', only in the Pre-Launch Reports intermittently.

EDIT: I have no ViewGroups in my Fragments with layout animations: Not animating ViewGroups

like image 794
grolschie Avatar asked Jan 09 '20 09:01

grolschie


Video Answer


1 Answers

This error can occur if you have a ViewGroup with animateLayoutChanges set to true in your ViewPager2's child fragments.

If this is the case, you need to tell that ViewGroup to only animate its own layout changes and not those of the ViewPager2, by calling

viewGroup.layoutTransition.setAnimateParentHierarchy(false)

as detailed in that answer.

The ViewPager2 documentation explains it :

If your pages contain LayoutTransitions, then those LayoutTransitions must have animateParentHierarchy set to false.

Note that if you have a ViewGroup with animateLayoutChanges="true" in your layout xml file, a LayoutTransition is added automatically to that ViewGroup.

You will need to manually call getLayoutTransition().setAnimateParentHierarchy(false) on that ViewGroup after you inflated the xml layout.

like image 165
Ronan Avatar answered Sep 20 '22 08:09

Ronan