NPE in Fragment.setUserVisibleHint() when using ViewPager

后端 未结 3 617
失恋的感觉
失恋的感觉 2021-01-11 17:32

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         


        
3条回答
  •  日久生厌
    2021-01-11 17:56

    This one was a real brain twister for me. We removed all of the code that replaced the Fragments 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.

提交回复
热议问题