I am at a loss for this. I am switching tabs manually in my ViewPager. I have this code inside my Activity:
@Override
public void onBackPressed()
{
if (c
Finally! I'm now able to reliably recreate this error!
Another way that I found to recreate error is to close activity/app, and quickly reopen page with the ViewPager fragment. You may have to try a few times because in my tests I had to reopen the app within about 30ms. This time may be slower or faster for different speed devices.
The problem was that I only explicitly created the Fragment (using new
) once, and kept a reference to that instance so that I could reuse it. One simple fix to this problem is to always return a new
instance of the Fragment the FragmentPagerAdapter.getItem(...)
, as shown below.
public class ViewPagerAdapter extends FragmentPagerAdapter {
...
@Override
public Fragment getItem(int position) {
switch (position) {
case 0: return mMyFragment; // Error. Has the edge-case crash.
case 1: return new MyFragment(); // Works.
default: return new MyDefaultFragment();
}
}
}
ps - The root problem likely has something to do with the Fragment lifecycle and trying to use it again while it's being destroyed.
pps - This solution fixes the NullPointerException for android.app.Fragment.setUserVisibleHint(Fragment.java:997)
and should also work for android.support.v4.app.Fragment.setUserVisibleHint
.
This one was a real brain twister for me. We removed all of the code that replaced the Fragment
s and kept the same fragments for the entire lifecycle of the Activity
and still had this problem. It wasn't until we viewPager.setOffscreenPageLimit(TABS);
where TABS
is the number of tabs (in our case 4) that we stopped getting the referenced NullPointerException
.
FWIW -- I'm pretty sure the problem is in Google's code. We couldn't get this to fail on a Nexus 5 running Lollipop, but it fails across Samsung devices running Kitkat. When I traced through the error itself, it looked like the failure happens because the Fragment
being referenced has already gone through the internal Fragment.initState
function because the Fragment
's id
is -1.
I had a bad time of a very similar case (same Exception in Fragment.setUserVisibleHint
) even if I used FragmentStatePagerAdapter. There was random (thus difficult to reproduce) crash of my app when the underlying datas where changed.
In my case, I finally was able to get rid of this crash by overriding the following method in the adapter :
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
// don't super !
}
I do think that there's a bug in this class, given the number of questions similar to this one in StackOverflow.
I hope this could be useful to someone.
Edit : I posted a similar answer to android.support.v4.app.Fragment.setUserVisibleHint null pointer on app resuming however, I'm not positively certain that these questions are duplicate. However, I think that this answer could help some people finding these for a similar symptom. How should I note this ?