Java.lang.illegalstateexception In The Viewpager After Data Update
Solution 1:
It looks like your adapter is sharing the list reference with your activity/fragment. What this means is: when this line executes
if (jsonParsed != null) mApodData.add(jsonParsed);
the list in the adapter is updated immediately since it is essentially the same list.
While the ViewPager
is doing its thing, it references the adapter again and again. So it checks that getCount()
and the adapter haven't changed in order to stay internally consistent.
The best way to fix this? if you know how many pages you will end up with, have your getCount()
method return that number. Another thing you can do is have the adapter create its own list and do a mList.addAll()
on the list from the activity. You could also set the ViewPager
adapter to null while retrieving the data from the server.
Solution 2:
Someone will jump into the following problem so I post it as one possible answer. To get the ViewPager to work perfectly is really an enormous timethief. Both the (missing) tutorials on developer.android.com and the code in FragmentPagerAdapter, they are both a disgrace. All those programmers around the world trying to find the narrow path to a fully working ViewPager!
I found one reason why you would get the illegal state in your logcat, stemming from the override of .destroyItem(), as follows: If you have a number of fixed fragments and e.g. start out to display fragment at position 0, having e.g. set the
viewPager.setOffscreenPageLimit (2);
Then if the user selects a new tab at position e.g. 4, the fragment at position 0 will be destroyed.
In this case, it is mandatory to call notifyDataSetChanged(), because the call to super (see below) will not synchronize the base FragmentPagerAdapter to your extension of it. Therefore, the logcat mysteriously says (with 7 fragments in my case):
"Expected adapter item count: 7, found: 6 Pager id: com.hdsl.a.zapp:id/viewpager Pager class: class android.support.v4.view.ViewPager Problematic adapter:"
The interpretation is: My extension of the base class has the correct count (6), while the base class does not (7). One should expect that a call to super would fix this, and it should, but it does not. Here is the correct code to counteract this bug:
@OverridepublicvoiddestroyItem(ViewGroup container, int position, Objectobject) {
super.destroyItem(container, position, object);//Note: does not help to move this to the end of the method
registeredFragmentsList.remove(position);
notifyDataSetChanged();
}
And to android core team: Call to super should be more intelligent! While this is not the case, please enhance the logcat with something much better, for example something like this: "The adapter found inconsistency in the number of fragments between FragmentPagerAdapter and the extended class. Make sure .notifyDataSetChanged() is always called whenever changes are made to your fragment list. Candidates are: (destroyItem, …..)"
"problematic adapter" is nothing but a push on the wild-guess button. Not helpful. In particular in this case, because the exception displayed in logcat does not contain a clue on where in your own code the error was provoked.
Post a Comment for "Java.lang.illegalstateexception In The Viewpager After Data Update"