Fragment Field is NULL after rotate device in Android

后端 未结 4 936
攒了一身酷
攒了一身酷 2021-01-23 11:39

When I start the app everything works ok but when I rotate to landscape it crashes because in the Fragment there is a field that is NULL.

I don

4条回答
  •  情话喂你
    2021-01-23 12:29

    this.mFragments = new ArrayList<>();
    

    Because this is wrong. You should never hold a reference to the fragment list if you are using ViewPager. Just return new instance of Fragment from getItem(int position) of FragmentPagerAdapter and you are good to go.

    But to fix your code, you must delete mFragments entirely.

    See https://stackoverflow.com/a/58605339/2413303 for more details.

提交回复
热议问题