Home » Android » android.support.v4.app.getFragmentManager() returns null?

android.support.v4.app.getFragmentManager() returns null?

Posted by: admin April 23, 2020 Leave a comment


This is my Stacktrace:

at com.example.test.fragments.LoadingFragment$1.run(LoadingFragment.java:66)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)

Line 66 of my Fragment:

FragmentTransaction ft = fm.beginTransaction();

fm is gotten like this:

android.support.v4.app.FragmentManager fm = getFragmentManager();

My Fragment extends android.support.v4.app.Fragment, these are my imports:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

i never had seen that i got null for a FragmentManager anybody got an Idea?

How to&Answers:

Replace this getFragmentManager() with this getSupportFragmentManager() and also make sure that your activity extends FragmentActivity


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;


CASE: When you need to call Fragment(Child fragment) from another Fragment(Parent Fragment)

always use getChildFragmentManager() instead of getFragmentManager() inside your Parent Fragment.


After cleaning and rebuilding my project aswell as restarting my testing device, this worked again somehow.