Trying to Remove Fragment from View Gives Me Nullpointerexception on Mnextanim

Trying to remove fragment from view gives me NullPointerException on mNextAnim

I know you've solved this yourself, but I want to explain how this error happens, since it just happened to me.

Basically you're calling hide(), remove(), etc., with a null value.

The error isn't obvious, and the stack trace doesn't originate from your own sources when this happens, so it's not trivial to realise what it is.

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.

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.

Crash with setnextanim?

Looking at the exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.support.v4.app.Fragment.setNextAnim(int)' on a null object
reference

and the fact that the stack trace contains no references to your code, I think it is reasonable to assume it is not related to this part of your code.

Rather, it is possibly related to some view being garbage collected/nulled somewhere down the line and then trying to use it later, calling show/hide or similar on a null object. See this discussion.

Why is getFragmentManager().findFragmentById returning null (causing NullPointerException)

For replacing/adding fragment use:

getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(R.anim.slide_in_top,
R.anim.slide_out_top)
.add(R.id.list_frame, fr, "tag").commit();

Now for finding this fragment use:

Fragment fr = getSupportFragmentManager()
.findFragmentByTag("tag");
if (fr != null) {
//do your stuff
} else {
//fr is null here
}


Related Topics



Leave a reply



Submit