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
What Is Difference Between @+Id/Android:List and @+Id/List
Send Sms Until It Is Successful
Change Listview Background - Strange Behaviour
Code Will Only Return 0.0, 0.0 Gps Coordinate While Throwing Nullpointerexception
Android Studio Convert Iso String to "America/New_York" When Adding to Event to Calendar
Full Screen Videoview Without Stretching the Video
How to Select and Crop an Image in Android
How to Get an Android User's Email Address
How to Programmatically Enable Gps in Android Cupcake
Androidruntime Error: Parcel: Unable to Marshal Value
How to Disable Sslv3 in Android for Httpsurlconnection
What Is Fatal Signal 6 in Android Logcat
How to Catch a Firebase Auth Specific Exceptions
Variable Length (Dynamic) Arrays in Java
In Java, Are Enum Types Inside a Class Static