Each time the new SDK creates an activity, it will automatically generate
public static class PlaceholderFragment extends Fragment
Fragment module. The nested fragment code is as follows:
public static class PlaceholderFragment extends Fragment {static FragmentManager fm;public PlaceholderFragment() { fm=getChildFragmentManager();}@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View rootView=LayoutInflater.from(getActivity()).inflate(R.layout.tab, null);if(fm.findFragmentByTag("haha")==null){Fragment1 f1=new Fragment1();fm.beginTransaction().add(R.id.frame_tab,f1,"haha").commit();}return rootView;}
The error message is as follows:
In short, the Acitivity is destroyed.
Cause:
Fm = getChildFragmentManager (); it is called in the PlaceholderFragment constructor. At this time, it has not created onCreate. Naturally, it cannot get the Fragment and activity, so it is written
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); fm=getChildFragmentManager(); }
To solve the error, you must pay attention to Fragment and activity lifecycle.