3

我自己无法重现此问题,但我在运行小米 MI 9 时注意到此问题Android 10。这是我使用的ViewPager2RecyclerView版本:

androidx.recyclerview:recyclerview:1.1.0 
androidx.viewpager2:viewpager2:1.0.0

这是堆栈跟踪:

Fatal Exception: java.lang.IllegalStateException: Design assumption violated.
       at androidx.viewpager2.adapter.FragmentStateAdapter.placeFragmentInViewHolder(FragmentStateAdapter.java:287)
       at androidx.viewpager2.adapter.FragmentStateAdapter.onViewAttachedToWindow(FragmentStateAdapter.java:276)
       at androidx.viewpager2.adapter.FragmentStateAdapter.onViewAttachedToWindow(FragmentStateAdapter.java:67)
       at androidx.recyclerview.widget.RecyclerView.dispatchChildAttached(RecyclerView.java:7556)
       at androidx.recyclerview.widget.RecyclerView$5.addView(RecyclerView.java:860)
       at androidx.recyclerview.widget.ChildHelper.addView(ChildHelper.java:107)
       at androidx.recyclerview.widget.RecyclerView$LayoutManager.addViewInt(RecyclerView.java:8601)
       at androidx.recyclerview.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:8559)
       at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1643)
       at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
       at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:675)
       at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
       at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
       at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
       at android.view.View.layout(View.java:21963)
       at android.view.ViewGroup.layout(ViewGroup.java:6317)
       at androidx.viewpager2.widget.ViewPager2.onLayout(ViewPager2.java:527)
       at android.view.View.layout(View.java:21963)
       at android.view.ViewGroup.layout(ViewGroup.java:6317)
       at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
       at android.view.View.layout(View.java:21963)
       at android.view.ViewGroup.layout(ViewGroup.java:6317)
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
       at android.view.View.layout(View.java:21963)
       at android.view.ViewGroup.layout(ViewGroup.java:6317)
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
       at android.view.View.layout(View.java:21963)
       at android.view.ViewGroup.layout(ViewGroup.java:6317)
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
       at android.view.View.layout(View.java:21963)
       at android.view.ViewGroup.layout(ViewGroup.java:6317)
       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
       at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
       at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
       at android.view.View.layout(View.java:21963)
       at android.view.ViewGroup.layout(ViewGroup.java:6317)
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
       at com.android.internal.policy.DecorView.onLayout(DecorView.java:803)
       at android.view.View.layout(View.java:21963)
       at android.view.ViewGroup.layout(ViewGroup.java:6317)
       at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3144)
       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2654)
       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1782)
       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7785)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1031)
       at android.view.Choreographer.doCallbacks(Choreographer.java:854)
       at android.view.Choreographer.doFrame(Choreographer.java:789)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1016)
       at android.os.Handler.handleCallback(Handler.java:883)
       at android.os.Handler.dispatchMessage(Handler.java:100)
       at android.os.Looper.loop(Looper.java:224)
       at android.app.ActivityThread.main(ActivityThread.java:7520)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

我不确定这是从我的代码中触发的,因为堆栈跟踪不是很清楚。此外,根据 Google Issuetracker,这已在 viewpager2:1.0.0-beta03: https://issuetracker.google.com/issues/134246546解决,但在我的情况下并不完全如此。这是另一篇具有相同问题的帖子,但没有多少有用的答案:FragmentStateAdapter throws java.lang.IllegalStateException: Design假设 violation after configuration change。如果有人知道为什么会发生这种情况或如何解决它,那就太好了。

4

2 回答 2

4

如果您覆盖,请getItemId()确保您也覆盖containsItem().

于 2020-06-22T21:30:20.500 回答
2

我尝试了所有解决方案,但没有奏效。我删除了 fragmentStateAdapter 中的 getItemId,它现在工作正常。

于 2020-01-27T14:36:28.180 回答