Skip to content Skip to sidebar Skip to footer

Illegalargumentexception: Only Tabitem Instances Can Be Added To Tablayout

I have a fragment1 and fragment2. Fragment1 is landing page of the app and Fragment2 has ViewPager with TabLayout:

Solution 1:

I added some logging and found that it crashes when TabLayout receives SlidingTabIndicator in addView method. I'm not sure what caused it but I resolved the crush with workaround:

I created new version of TabLayout and changed addView methods so I could filter all the views different form those which are expected there:

classNewTabLayout : TabLayout {constructor(context: Context) : super(context)
    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)
    constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) :
        super(context, attributeSet, defStyleAttr)

    overridefunaddView(child: View?, width: Int, height: Int) {
        if (child is TabItem) {
            super.addView(child, width, height)
        }
    }

    overridefunaddView(child: View?, width: Int) {
        if (child is TabItem) {
            super.addView(child, width, height)
        }
    }

    overridefunaddView(child: View?) {
        if (child is TabItem) {
            super.addView(child, width, height)
        }
    }
}

Post a Comment for "Illegalargumentexception: Only Tabitem Instances Can Be Added To Tablayout"