waseefakhtar
waseefakhtar

Reputation: 1423

ViewPager2 Exception: java.lang.IllegalStateException: Design assumption violated

I can't reproduce this issue myself but I notice this issue in production with Xiaomi MI 9 running Android 10. Here are the ViewPager2 and RecyclerView versions I use:

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

And here's the stacktrace:

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)

I'm not sure where this is triggered from my code since the stacktrace is not very clear. Also, as per Google Issuetracker, this has been solved in viewpager2:1.0.0-beta03: https://issuetracker.google.com/issues/134246546 with the commit here but not quite in my case. And here's another post with the same issue with not many helpful answers: FragmentStateAdapter throws java.lang.IllegalStateException: Design assumption violated after configuration change. It'd be great if anyone knows why this occurs or how it can be solved.

Upvotes: 3

Views: 4440

Answers (2)

Westy92
Westy92

Reputation: 21335

If you override getItemId() make sure you also override containsItem().

Upvotes: 8

Oğuz Ayan
Oğuz Ayan

Reputation: 31

I tried everything for the solution but it did not work. I removed the getItemId in the fragmentStateAdapter and it is working fine now.

Upvotes: 3

Related Questions