Android Fragmentmanager Backstackrecord.Run Throwing Nullpointerexception

Android FragmentManager BackStackRecord.run throwing NullPointerException

Answering my own question:

This exception is (eventually) thrown when you call FragmentTransaction.remove(null); and FragmentTransaction.commit();

EDIT: And also, like Twice Circled and shinyuX point out in the comment; when calling the show(null) or add(null), attach(null) and detach(null) methods, and probably also hide(null)

After calling commit(), the transaction will be queued in the FragmentManager. As a result, when the operation is being processed after you explicitly call FragmentManager.executePendingTransactions(), or when the FragmentManager queue thread calls it, it throws a NullPointerException.

In my case, I was maintaining fragment states in a global object. There I checked if the fragment was showing or not, and then removed visible fragments. But because I started a new FragmentActivity, these states were still set to true while they were not visible. So this is a design error.

Other than fixing the design error, the solution was simple: check whether FragmentManager.findFragmentByTag() returned null before removing the fragment.

Fragment manager throws nullPointerException

I have just solved it.

Just using supportFragmentManager instead of FragmentManager
and support.v4.fragment instead of app.fragment.

NullPointerException in FragmentManager

Ok guys, after hitting my head against a brick wall for a while on this I found that this was directly associated with my fragment's declaration of setRetainInstance(true). After removing this the problems went away. This appears to be a compatibility library bug...

I will raise something under the appropriate Google project.
Best of luck if you are reading this slowly sobbering to yourself! I hope this will allow you to work around the problem.

Attempt to refresh fragment gives me NullPointerException; I've made sure compatibility isn't an issue, and my fragments shouldn't be null

Update:
In the switch statement, you are missing the break; statements on your cases.

A glance at line 722 of the source code for BackStackRecord suggests you called FragmentTransaction.detach() with a null fragment. Probably one of your calls to findFragmentById() is returning null. To find out which one, check the result of each call and log a debug message if it is null.

NullPointerException when loading fragments

Functions show and hide works on existing fragments

Hides an existing fragment. This is only relevant for fragments whose views have been added to a container

Shows a previously hidden fragment. This is only relevant for fragments whose views have been added to a container

But fragments have separate life-cycle than activity and they may not be created and attached yet when activity's onCreate is called.

Check for null when You call Your changeFragment or inside of changeFragment.

If You want one of the fragments to be hidden on the beginning consider makeing a callback call from fragment in onAttached() method or add fragments manually instead of xml.

My preferred option is to do add fragments manually.

Null pointer exception when loading fragment

In your Activity you are importing android.support.v4.app.Fragment and your Fragment is android.app.Fragment. That's why your cast fails after calling newInstance() and your fragment is null. The same goes with your ListFragment.

You also need to implement newInstance() in your fragment.

public static Fragment newInstance() 
{
FragmentAudio myFragment = new FragmentAudio();
return myFragment;
}


Related Topics



Leave a reply



Submit