I use android.support.design in all my projects, and today I updated it to latest version
com.android.support:design:22.2.1
It was a bad idea, because my project crashed with
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
java.util.ArrayList.get(ArrayList.java:308)
android.support.design.widget.TabLayout.getTabAt(TabLayout.java:407)
android.support.design.widget.TabLayout.setupWithViewPager(TabLayout.java:585)
After some debugging, I found a problem in this method:
public void setupWithViewPager(@NonNull ViewPager viewPager) {
PagerAdapter adapter = viewPager.getAdapter();
if(adapter == null) {
throw new IllegalArgumentException("ViewPager does not have a PagerAdapter set");
} else {
this.setTabsFromPagerAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(this));
this.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
if(this.mSelectedTab == null || this.mSelectedTab.getPosition() != viewPager.getCurrentItem()) {
this.getTabAt(viewPager.getCurrentItem()).select();// ERROR On my code I'm used empty adapter
}
}
}
But in previous version 22.2.0 of lib, all was working fine:
public void setupWithViewPager(ViewPager viewPager) {
PagerAdapter adapter = viewPager.getAdapter();
if(adapter == null) {
throw new IllegalArgumentException("ViewPager does not have a PagerAdapter set");
} else {
this.setTabsFromPagerAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(this));
this.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
}
}
Who did it and who can be punished for it?
Please help me find developers for Android support Design library.
viewPager.getCurrentItem()
is not zero-indexed and the method,getTabAt()
expects it to be. Check the APIs, trythis.getTabAt(viewPager.getCurrentItem() - 1).select();
– AustroasiaticgetCurrentItem()
onViewPager
is zero-based. – Compliancy