天天看點

android.support.v4.app.FragmentHostCallback.getHandler()' on a null object reference1. Bug問題所在2. Bug出現場景3. Bug解決辦法

1. Bug問題所在

  • ViewPager為null或者TabLayout為null

2. Bug出現場景

  • TabLayout+ViewPager包裹Fragment
  • Fragment裡面又嵌套了TabLaytou+ViewPager包裹Fragment
  • 裡層或者外層的Fragment的Adaper繼承的是FragmentStatePagerAdaper

3. Bug解決辦法

  • 裡層和外層的Fragment都使用FragmentPagerAdaper(個人測試OK)
  • 在Fragment的Adapter中重寫以下方法
@Override
public void finishUpdate(ViewGroup container) {
    try{
        super.finishUpdate(container);
    } catch (NullPointerException nullPointerException){
        System.out.println("Catch the NullPointerException in FragmentPagerAdapter.finishUpdate");
    }
}
           

繼續閱讀