java.lang.IllegalStateException:页面只能偏移一个正数 [英] java.lang.IllegalStateException: Page can only be offset by a positive amount

查看:970
本文介绍了java.lang.IllegalStateException:页面只能偏移一个正数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在发布前报告中遇到此错误,但未显示对.java文件的任何引用。任何人都可以对此进行说明吗?



我在页面上使用 ViewPager2 (1.0.0)

 问题:java.lang.IllegalStateException:页面只能偏移一个正数,不能偏移-54 
致命异常:主
流程:[已编辑],PID:18424
java.lang.IllegalStateException:页面只能以正数偏移,而不能在androidx处以-54
偏移。 viewpager2.widget.ScrollEventAdapter.updateScrollEventValues(ScrollEventAdapter.java:280)
在androidx.viewpager2.widget.ScrollEventAdapter.onScrolled(ScrollEventAdapter.java:178)
在androidx.recyclerview.widget.RecyclerView.dispatchOnScrolled( RecyclerView.java:5173)
在androidx.recyclerview.widget.RecyclerView $ ViewFlinger.run(RecyclerView.java:5338)
在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:858) android.view.Choreographer.doCallbacks(Choreographer.java:670)中的
android.view.Ch中的
oreographer.doFrame(Choreographer.java:603)
在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:844)
在android.os.Handler.handleCallback(Handler.java:746) android.os.Handler.dispatchMessage(Handler.java:95)的
android.os.Looper.loop(Looper.java:148)的
android.app.ActivityThread.main的
(ActivityThread.java:5459)java.lang.reflect.Method.invoke(Method.java)
(com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:728) )
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

我发现。



ViewPa ger2文档对此进行了解释:


如果您的页面包含LayoutTransitions,则这些LayoutTransitions
必须将animateParentHierarchy设置为false 。



请注意,如果布局xml文件中具有animateLayoutChanges = true的
a ViewGroup,则
a LayoutTransition将自动添加到该ViewGroup。



膨胀XML布局后,您需要
对该
ViewGroup手动调用
getLayoutTransition()。setAnimateParentHierarchy(false) 。



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:

解决方案

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.

这篇关于java.lang.IllegalStateException:页面只能偏移一个正数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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