So, this is a difficult question, but I will try to make it as simple as possible.
To get started, I have a basic action with a view pager. It has 2 pages, with two tabs in the action bar linking each page. I also have a box that has 4 options, the first 2 - 2 pages in the presentation pager, and the second 2 are pages that are not. If you choose one of the second 2, I configured my ViewPager to replace the fragments with 2 others. All these fragments are single-point, so switching back and forth does not cause memory problems.
When switching to the second set of fragments, one of the fragments has a frame in it, which I use to replace child fragments when I select a button. This is where the problem lies. After this fragment is loaded for the first time, it works fine. But if you use the box to switch to the first set, and then again to the second, the child fragment has disappeared. Then it tends to crash after you do something else after that.
This is a crash log for him
01-10 15:55:05.272: E/MessageQueue-JNI(21034): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(android.support.v4.app.Fragment)' on a null object reference
If I play a little with the code, I get something like this. These two errors are the ones that I seem to produce the most:
01-10 16:00:13.072: E/AndroidRuntime(22227): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.ClassLoader android.support.v4.app.FragmentActivity.getClassLoader()' on a null object reference
I can continue to call replace () in the frame view so that it does not go away. Therefore, I assume that this is not a problem. it
Here is the appropriate code for this.
Fragment Code:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.calculator, container, false); final FragmentManager fragmentManager = getChildFragmentManager(); if(afFrag == null && topFrag == null){ afFrag = CalculatorAfFragment.getInstance(); topFrag = CalculatorTopFragment.getInstance(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.calc_frame, afFrag, "afFrag").commit(); } RadioGroup typeGroup = (RadioGroup) view.findViewById(R.id.type_buttons); typeGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { if (checkedId == R.id.af_button){ FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.calc_frame, afFrag, "afFrag").commit(); } else if (checkedId == R.id.top_button){ FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.calc_frame, topFrag, "topFrag").commit(); } } }); return view;
FragmentStatePagerAdapter Code:
@Override public Fragment getItem(int position) { if(tools){ return toolsFragmentsList.get(position); } else { return guidesFragmentsList.get(position); } } @Override public int getItemPosition(Object object) { return POSITION_NONE; } public void swapFragments(int position){ if (tools){ tools = false; stores = false; notifyDataSetChanged(); this.setPrimaryItem(mPager, position-1, getItem(position - 1)); } else { tools = true; notifyDataSetChanged(); this.setPrimaryItem(mPager, position-4, getItem(position - 4)); } }