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 the reoson why my last edit is placed on top of my question.
But this led to the next error:
java.lang.NullPointerException at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:569) at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:211) at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1281) at android.view.View.dispatchRestoreInstanceState(View.java:12043) at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2688) at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2694) at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2694) at android.view.View.restoreHierarchyState(View.java:12021) at android.support.v4.app.Fragment.restoreViewState(Fragment.java:425) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:949) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440) at android.os.Handler.handleCallback(Handler.java:615) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4800) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565) at dalvik.system.NativeStart.main(Native Method)
And now I don't know where to continue...
Edit 0 - FIRST question
I get following exception:
java.lang.IllegalStateException: No activity at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1091) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086) at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1877) at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1492) at android.support.v4.app._HoloFragment.performActivityCreated(_HoloFragment.java:251) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440) at android.os.Handler.handleCallback(Handler.java:615) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4800) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565) at dalvik.system.NativeStart.main(Native Method)
I have no idea where to start. I'm using the up to date support library (v4, rev18).
Everything works fine until I add a Fragment
which holds a ViewPager
in combination with FragmentStatePagerAdapter
.
I'm using the support library everywhere, I tried the solutions from https://code.google.com/p/android/issues/detail?id=42601...
But nothing worked yet...
I don't know where to start, does anyone have a hint?
EDIT 1: BACKGROUND and Code
1) In my MainActivity I use following:
The third page has the viewpager...
public void setPage(int pos, boolean addToBackStack) { Fragment nextFragment = null; FragmentTransaction transaction = null; switch (pos) { case 0: if (mFragmentAllRoutines == null) mFragmentAllRoutines = new RoutinesListFragment(); if (!mFragmentAllRoutines.isVisible()) nextFragment = mFragmentAllRoutines; break; case 1: if (mFragmentRoutine == null) mFragmentRoutine = new RoutineFragment(); if (!mFragmentRoutine.isVisible()) nextFragment = mFragmentRoutine; break; case 2: if (mFragmentDay == null) mFragmentDay = new RoutineDayFragment(); if (!mFragmentDay.isVisible()) nextFragment = mFragmentDay; break; default: break; } if (nextFragment != null) { // Fragment prevFragment = getSupportFragmentManager().findFragmentById(R.id.fragment); transaction = getSupportFragmentManager().beginTransaction(); // if (prevFragment != null) // transaction.detach(prevFragment); transaction.replace(R.id.fragment, nextFragment, nextFragment.getClass().getName()); // transaction.attach(nextFragment); if (addToBackStack) transaction.addToBackStack(null); transaction.commit(); } }
2) the third fragment:
It's a simple Fragment with a ViewPager and a FragmentStatePagerAdapter. The FragmentStatePagerAdapter get's the getChildFragmentManager()
as FragmentManager. And somewhere there seems to be the problem.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With