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

梦想的初衷 提交于 2020-02-21 05:53:46

问题


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.


回答1:


It's an bug in ViewPager2, which is caused when calculating the offset of the first item in RecyclerView. Try removing start/left side separator, and paddingLeft/paddingStart.

Source



来源:https://stackoverflow.com/questions/59660691/java-lang-illegalstateexception-page-can-only-be-offset-by-a-positive-amount

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!