Reputation: 996
I am using TabLayout
from the design library with the ViewPager
, linking them using the function setupWithViewPager
. The app is crashing in scenarios where it recreates the tabs, after the tab contents have been changed. Crash trace :
java.lang.IllegalArgumentException: Tab belongs to a different TabLayout.
at android.support.design.widget.TabLayout.addTab(TabLayout.java:433)
at android.support.design.widget.TabLayout.populateFromPagerAdapter(TabLayout.java:772)
at android.support.design.widget.TabLayout.setPagerAdapter(TabLayout.java:763)
at android.support.design.widget.TabLayout.setupWithViewPager(TabLayout.java:715)
The crash is occuring after updating to support library 23.2.0, doesn't reproduce till v23.1.1.
Upvotes: 7
Views: 2430
Reputation: 144
I have meet the same problem, and then I found the newer TabLayout
use a pool to cache Tab.
in 23.1.1
public Tab newTab() {
return new Tab(this);
}
and in 23.2.0
public Tab newTab() {
Tab tab = sTabPool.acquire();
if (tab == null) {
tab = new Tab(this);
}
tab.mView = createTabView(tab);
return tab;
}
so if you use newTab()
to create a Tab, and for some reason you didn't add it to the TableLayout
. the next time you enter another activity with a TabLayout
, this would happen.
Upvotes: 1
Reputation: 557
I can still see this issue in support lib version: 25.3.1. So to avoid the crash, removedAllTabs() and created a new instance for the tab again and added to Tablayout.
gauge_tab.removeAllTabs()
gauge_tab.addTab(gauge_tab.newTab().setText(R.string.flash_gauge_04))
gauge_tab.addTab(gauge_tab.newTab().setText(R.string.flash_gauge_06))
gauge_tab.addTab(gauge_tab.newTab().setText(R.string.flash_gauge_08))
Upvotes: 0
Reputation: 22945
This was bug reported on google https://code.google.com/p/android/issues/detail?id=201827
But after release of Android Support Library, revision 23.2.1 (March 2016) This is fixed now.
just update Support Library to Android Support Library to 23.2.1
Upvotes: 2
Reputation: 996
Just found that this is an internal bug in Support library v23.2.0, registered at : https://code.google.com/p/android/issues/detail?id=201827
Upvotes: 7