IllegalStateException: The application's PagerAdapter changed the adapter's content without calling PagerAdapter#notifyDataSetChanged
I had a hard time making my ViewPager
working. At the end, it seems that the example in the documentation is wrong.
The addTab
method should be as follows:
public void addTab(Tab tab, Class<?> clss, Bundle args) {
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
notifyDataSetChanged();
mActionBar.addTab(tab);
}
Notice the order of the last three operations. In the original example, notifyDataSetChanged
was called after the mActionBar.addTab
function.
Unfortunately, as soon as you call the addTab
on the ActionBar
, the first tab you add is automatically selected. Because of this, the onTabSelected
event is fired and while trying to retrieve the page, it throws the IllegalStateException
because it notices a discrepancy between the expected item count and the actual one.
java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged
Using clone()
method to copy the data from the ArrayList
which i passed to the constructor of ViewPager's adapter
finally solved the issue.
Related Topics
Get Temperature of Battery on Android
Android Device as a Receiver for A2Dp Profile
Fire a Pinch In/Out Command to Android Phone Using Adb
Android Background Service Is Restarting When Application Is Killed
Display All Unicode Chars in Textview
Fragment-Fragment Communication in Android
Resource Linking Fails on Lstar
Difference Between Finish() and System.Exit(0)
Android/Java: Transition/Migration from Proguard to R8
Android.Os.Networkonmainthreadexception with Android 4.2
How to Open Phones Gallery Through Code
Launch Android Application from a Browser Link
Quality Selector for Exoplayer 2
Java.Lang.Illegalargumentexception: Can Only Use Lower 16 Bits for Requestcode
Android Convert Date and Time to Milliseconds
Is My App or Its Dependencies Violating the Android Advertising Id Policy
Call to 'Getdrawingcache' Returns Null When Scroll Is Enabled