3

I have a random crash on my application, and look they are on android internal code. One is:

    java.lang.IllegalArgumentException: Only TabItem instances can be added to TabLayout
        at com.google.android.material.tabs.TabLayout.addViewInternal(TabLayout.java:1606)
        at com.google.android.material.tabs.TabLayout.addView(TabLayout.java:1589)
        at android.transition.TransitionUtils.createViewBitmap(TransitionUtils.java:192)
        at android.transition.TransitionUtils.copyViewImage(TransitionUtils.java:103)

and the other

    java.lang.IllegalStateException: ViewPager2 does not support direct child views
        at androidx.viewpager2.widget.ViewPager2.onViewAdded(ViewPager2.java:489)
        at android.view.ViewGroup.dispatchViewAdded(ViewGroup.java:5937)
        at android.view.ViewGroup.addViewInner(ViewGroup.java:6119)
        at android.view.ViewGroup.addView(ViewGroup.java:5884)
        at android.view.ViewGroup.addView(ViewGroup.java:5824)
        at android.transition.TransitionUtils.createViewBitmap(TransitionUtils.java:192)
        at android.transition.TransitionUtils.copyViewImage(TransitionUtils.java:103)

none of them looks to be in my code... has anyone encountered these crashes before?

Edit:

the remainder of stacktraces are common:

        at android.transition.TransitionUtils.copyViewImage(TransitionUtils.java:103)
        at android.transition.Visibility.onDisappear(Visibility.java:420)
        at android.transition.Visibility.createAnimator(Visibility.java:255)
        at android.transition.Transition.createAnimators(Transition.java:733)
        at android.transition.TransitionSet.createAnimators(TransitionSet.java:444)
        at android.transition.Transition.playTransition(Transition.java:1777)
        at android.transition.TransitionManager$MultiListener.onPreDraw(TransitionManager.java:327)
        at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:1124)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3867)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2631)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:9978)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1010)
        at android.view.Choreographer.doCallbacks(Choreographer.java:809)
        at android.view.Choreographer.doFrame(Choreographer.java:744)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:995)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8506)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

it happens on android 10 and not on android 7-9, it looks like it is trying to create bitmap of current fragment, but the current fragment contains TabLayout and viewPager and are not supported. Debugging I see that the invalid view it tries to add on TabLayout is a SlidingTabIndicator, so it looks correct. Another information, It looks like the crash started when I moved from Theme.AppCompat.Light.NoActionBar to Theme.MaterialComponents.Light.NoActionBar as base theme of my application

Perry
  • 1,113
  • 2
  • 10
  • 22
  • 1
    It looks like the problem is that before exit from fragment it does `tabLayout.visibility = View.GONE` without this, it does not crash, but I have an unaesthetic empty tab layout – Perry Jun 15 '21 at 09:46
  • It is clearly a bug on code base of android – Perry Jun 25 '21 at 13:29
  • hey @Perry have you resolved the problem? – Rainmaker Aug 17 '21 at 22:08
  • I did a workaround: when exit page if the tabLayout is gone then set it to visible – Perry Aug 19 '21 at 10:39
  • I also got this issue and solved it with another workaround. You can check it here :) https://stackoverflow.com/a/68852671/2339405 – Rainmaker Aug 19 '21 at 18:06

1 Answers1

0

Based on the given stack traces...

ViewPager2 does not support direct child views: As the error message clearly states, do NOT add fragments directly to ViewPager2.

For example, calling getFragmentManager().beginTransaction().replace will not work. If you want to add Fragments to Viewpagers, you should use FragmentStateAdapter .

Only TabItem instances can be added to TabLayout: As the error message states, make sure your TabLayout in XML does NOT have any child other than TabItem.

For example this is incorrect:

<android.support.design.widget.TabLayout">
     <android.support.v4.view.ViewPager /> <!-- This Viewpager should be placed somewhere else. -->
</android.support.design.widget.TabLayout>

Now this is correct:

<android.support.design.widget.TabLayout">
  <!-- No Child allowed other than TabItem. -->
</android.support.design.widget.TabLayout>

If you think this is not the answer, please provide more details.

Let me know if you have any questions.

minchaej
  • 1,294
  • 1
  • 7
  • 14