Android.support.v4.app.getFragmentManager() Returns Null?
This is my Stacktrace: FATAL EXCEPTION: main java.lang.NullPointerException at com.example.test.fragments.LoadingFragment$1.run(LoadingFragment.java:66) at android.os.Handler.handl
Solution 1:
Replace this getFragmentManager()
with this getSupportFragmentManager()
and also make sure that your activity extends FragmentActivity
Solution 2:
From android.support.v4.app.Fragment
source:
// The fragment manager we are associated with. Set as soon as the
// fragment is used in a transaction; cleared after it has been removed
// from all transactions.
FragmentManagerImpl mFragmentManager;
Apparently getFragmentManager()
returns support version of fragment manager but only during a transaction (or parent fragment's child fragment manager).
I'm going to use this:
public FragmentManager getHostFragmentManager() {
FragmentManager fm = getFragmentManager();
if (fm == null && isAdded()) {
fm = ((AppCompatActivity)getActivity()).getSupportFragmentManager();
}
return fm;
}
Solution 3:
CASE: When you need to call Fragment(Child fragment) from another Fragment(Parent Fragment)
always use getChildFragmentManager()
instead of getFragmentManager()
inside your Parent Fragment.
Solution 4:
After cleaning and rebuilding my project aswell as restarting my testing device, this worked again somehow.
Post a Comment for "Android.support.v4.app.getFragmentManager() Returns Null?"