Starting Activity from Fragment causes NullPointerException

后端 未结 4 1067
醉话见心
醉话见心 2020-11-28 02:39

I\'m stuck here with a problem starting an activity from a Button inside a Fragment. Here is my code:

ViewPagerAdapter.java

import j         


        
相关标签:
4条回答
  • 2020-11-28 02:49

    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 :)

    0 讨论(0)
  • 2020-11-28 02:50

    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

    0 讨论(0)
  • 2020-11-28 02:58

    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.

    0 讨论(0)
  • 2020-11-28 03:07

    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");
    }
    
    0 讨论(0)
提交回复
热议问题