I\'m stuck here with a problem starting an activity from a Button inside a Fragment. Here is my code:
ViewPagerAdapter.java
import j
Quick fix is
class MyFragment extends Fragment {
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
setUserVisibleHint(true);
}
And wait for update in safe mode :)
This is a known problem and I'm shocked to find that no one has filed a bug on the public bug tracker for this.
Your problem stems from this:
if (f.mSavedViewState != null) {
if (result == null) {
result = new Bundle();
}
result.putSparseParcelableArray(
FragmentManagerImpl.VIEW_STATE_TAG, f.mSavedViewState);
}
if (!f.mUserVisibleHint) {
// Only add this if it's not the default value
result.putBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG, f.mUserVisibleHint);
}
You should notice that if result
is null
coming into this code and the first if
is not triggered you'll get a NullPointerException
if we enter the second if
.
It should read:
if (f.mSavedViewState != null) {
if (result == null) {
result = new Bundle();
}
result.putSparseParcelableArray(
FragmentManagerImpl.VIEW_STATE_TAG, f.mSavedViewState);
}
if (!f.mUserVisibleHint) {
if (result == null) {
result = new Bundle();
}
// Only add this if it's not the default value
result.putBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG, f.mUserVisibleHint);
}
I submitted a patch for this a week or two ago: https://android-review.googlesource.com/31261
According to a post here, you should be using FragmentStatePagerAdapter rather than FragmentPagerAdapter. I had the same problem you described, and making that switch worked for me.
I just had the same problem - the issue for me was caused when I added a new fragment to my project with an empty layout. It seems that if nothing is saved to the bundle during Fragment.onSaveInstanceState(Bundle outState)
it causes a null pointer exception in FragmentManagerImpl.saveFragmentBasicState
.
Adding some views to my layouts fixed the issue (after hours spent bashing my head against a wall). Using android support library v4 revision 10.
Also when using a ListView
as the root for the fragment layout, if the listview has no elements you will also get the crash. In the fragment class, override onSaveInstanceState
like this to put some data in the bundle:
@Override
public void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putString("DO NOT CRASH", "OK");
}