EDIT 2
I now managed to get rid of the error with using the trick from here https://code.google.com/p/android/issues/detail?id=42601#c10 so that\'s
I would need to see more code in order to know where to apply this ... but I had a similar situation as you did... I solved the ChildFragmentManager
issue the same way, and got the same NullPointerException
, as you have.
I did manage to solve it... I'll try to describe my solution, as with the code you supplied I am unsure as to where to apply the "fix" ... It really is a fix ... it's not an elegant way to solve this! It did however helped me, and I don't need to restore states once I navigate away on the view Pager, so no downside for me.
I have multiple Fragments, that implement the static bit of code and the onDetach (on https://code.google.com/p/android/issues/detail?id=42601#c10), each fragment has a pager, and onCreateView I attach an adapter that extends FragmentStatePagerAdapter.
on those adapters I have overridden the restoreState method to do nothing
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
//do nothing here! no call to super.restoreState(arg0, arg1);
}
Again, this is not the best way... but it did solve my problem.
Best of luck!
First of all, English is not my mother tongue, if I have something wrong in grammar,please tell me. I encountered this problem in my project. At first I did what Marc said, and it worked well. But when I added another ViewPager in a father fragment, the new ViewPager showed as same as the old one no matter what kinda code in this new viewpager. At last I found the reason. We always create Fragment by using FragmentTrasaction.replace(R.layout.fragment,fragment) method. Because of this, we create a new fragment instance when we use this method. Instead of using replace() method, I use FragmentTransaction.hide() and show() method to switch fragment. I solved my problem this way. Here is my code snippets :
@Override
public void onClick(View v) {
FragmentTransaction transaction = fm.beginTransaction();
switch (v.getId()) {
case R.id.tv_schedule_day:
hideFragment(transaction);
if (dayFragment == null) {
dayFragment = new ScheduleDayFragment();
transaction.add(R.id.ll_schedule_content, dayFragment);
} else {
transaction.show(dayFragment);
}
transaction.commit();
break;
case R.id.tv_schedule_week:
hideFragment(transaction);
if (weekFragment == null) {
weekFragment = new ScheduleWeekFragment();
transaction.add(R.id.ll_schedule_content, weekFragment);
} else {
transaction.show(weekFragment);
}
transaction.commit();
break;
case R.id.tv_schedule_month:
hideFragment(transaction);
if (monthFragment == null) {
monthFragment = new ScheduleMonthFragment();
transaction.add(R.id.ll_schedule_content, monthFragment);
} else {
transaction.show(monthFragment);
}
transaction.commit();
break;
private void hideFragment(FragmentTransaction transaction) {
if (dayFragment != null) {
transaction.hide(dayFragment);
}
if (weekFragment != null) {
transaction.hide(weekFragment);
}
if (monthFragment != null) {
transaction.hide(monthFragment);
}
}
I use hide() and show() methods to make switch father fragment, so when I use getChildFragmentManager() in embedded fragment, it wolud not return null. This question was asked in 2013, I think no one would see this answer, but I am still very glad to anwser this question.
I have just stumbled upon the same problem.
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:873)
at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:215)
The solution by Marc works but in my case it caused lags on the Fragment with FragmentStatePagerAdapter. So I overrode restoreState, called super, wrapped it in try/catch and it worked with no lags.
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
try{
super.restoreState(state, loader);
}catch (NullPointerException e){
// null caught
}
}
UPDATE:
I Have been using this solution in production since then. So it is safe to use.
On my side, I was keeping a reference to the ViewPager fragments in a containing fragment and was using onSaveInstanceState to store their states. I think the NPE occurred because this was colliding with the ViewPager own mechanism to restore the fragments. What I did, change my architecture from a ViewPager to fragment transactions I manage myself. (ViewPager was not an appropriate solution for my case)