Fragment has not been attached yet

前端 未结 1 1886
攒了一身酷
攒了一身酷 2020-12-21 11:05

After reading posts on this exception, I can\'t understand what I need to do to correct this error. Besides, I can\'t even reproduce it... This happens sometimes on some dev

相关标签:
1条回答
  • 2020-12-21 11:24

    Your app crashes because the implementation of FragmentStatePagerAdapter is incorrect.

    // private var fragments: MutableList<Fragment> = mutableListOf() // <-- remove this line entirely
    

    You mustn't keep a list of fragments in your Fragment*PagerAdapter, because the system recreates the Fragments, and the one you'll be invoking in that list will never actually be added to the FragmentManager.

    Something you can do is switch FragmentStatePagerAdapter to FragmentPagerAdapter, then you can apply App crash after activity has been killed in background and How to get elements of fragments created by ViewPager in MainActivity? .

    To reproduce your crash, refer to Singleton object becomes null after app is resumed .

    0 讨论(0)
提交回复
热议问题