NullPointerException in Fragment's onCreateView() method

前端 未结 2 1911
情书的邮戳
情书的邮戳 2020-12-21 06:53

My code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // t-alk és impresszumhoz
         


        
2条回答
  •  隐瞒了意图╮
    2020-12-21 07:28

    Here are the internals of the Fragment life cycle:


    FragmentManager sets the activity value for a Fragment performing a state transition:

    f.mActivity = mActivity;
    f.mFragmentManager = mActivity.mFragments;
    f.mCalled = false;
    f.onAttach(mActivity);
    

    Also, it sets this value to null, after detaching the Fragment:

    f.mCalled = false;
    f.onDetach();
    
    . . .
    
    f.mActivity = null;
    f.mFragmentManager = null;
    

    So, The value should not be null, between onAttach() and onDetach(), if every thing else is fine.


    To be safe, move all such code to onActivityCreated();

提交回复
热议问题