Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException in FragmentManager

I'm using the Android compatibility library and occasionally get a weird NullPointerException:

java.lang.NullPointerException     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:960)     at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:768)     at android.support.v4.app.FragmentManagerImpl.startPendingDeferredFragments(FragmentManager.java:1104)     at android.support.v4.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:410)     at android.support.v4.content.Loader.deliverResult(Loader.java:103)     at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:81)     at android.support.v4.content.CursorLoader.onStartLoading(CursorLoader.java:126)     at android.support.v4.content.Loader.startLoading(Loader.java:197)     at android.support.v4.app.LoaderManagerImpl$LoaderInfo.start(LoaderManager.java:262)     at android.support.v4.app.LoaderManagerImpl.doStart(LoaderManager.java:710)     at android.support.v4.app.Fragment.onStart(Fragment.java:981)     at android.support.v4.app.Fragment.performStart(Fragment.java:1332)     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:906)     at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1240)     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:612)     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:431)     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:139)     at android.support.v4.view.ViewPager.populate(ViewPager.java:804)     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:433)     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:405)     at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:386)     ... 

Obviously, I'm doing something wrong to allow the FragmentManager to get into such a state where it can crash like this, but I have no clue what. The relevant code in the FragmentManagerImpl is not offering me any clues. I'm guessing mActivity is somehow null at that point in the code? But that seems impossible as the activity is already on the screen and I'm not adding any fragments to it — just switching among them in a ViewPager.

like image 872
mlc Avatar asked May 04 '12 21:05

mlc


People also ask

What is a NullPointerException?

NullPointerException is a RuntimeException. In Java, a special null value can be assigned to an object reference. NullPointerException is thrown when program attempts to use an object reference that has the null value.

How do I use the fragmentmanager?

The appropriate FragmentManager property to reference depends on where the callsite is in the fragment hierarchy along with which fragment manager you are trying to access. Once you have a reference to the FragmentManager, you can use it to manipulate the fragments being displayed to the user.

What is the use of getchildfragmentmanager () inside a fragment?

Fragments are also capable of hosting one or more child fragments. Inside a fragment, you can get a reference to the FragmentManager that manages the fragment's children through getChildFragmentManager () .

How do I replace a fragment in a fragmenttransaction?

For example, a simple FragmentTransaction might look like this: In this example, ExampleFragment replaces the fragment, if any, that is currently in the layout container identified by the R.id.fragment_container ID. Providing the fragment's class to the replace () method allows the FragmentManager to handle instantiation using its FragmentFactory .


2 Answers

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.

like image 62
BrantApps Avatar answered Oct 05 '22 15:10

BrantApps


I got this error a few days ago and was quite confounded, but I figured out that it was because a FragmentTransaction didn't seem to get any pending transactions so when calling executePendingTransactions(); A NPE much like this one was thrown.
I solved it by making sure that every transaction actually changed something (i.e add fragment 1 -> remove fragment 1 -> commit/execute would not work).
Also make sure that none of the fragments in the pager have become null.

like image 39
Jave Avatar answered Oct 05 '22 13:10

Jave