导航架构组件:FragmentManager已经在执行事务

问题描述 投票:0回答:1

我有带有3个主要片段的BottomNavigationView:A,B和C。当我尝试使用BottomNavigationView按任何顺序第二次打开FragmentC时,我看到一个错误:FragmentManager已经在执行事务了

BUT没有setupViewPager()行,一切正常,我不止一次打开FragmentC。否则,将显示错误。

我不知道发生了什么。拜托,我真的需要您的帮助。

setViews()这是在BaseFragment中定义并在onViewCreated()]中调用的函数

FragmentC

class FragmentC: BaseFragment() {

    override val menuResId: Nothing? = null
    override val contentResId = R.layout.fragment_c
    override val baseToolbar = R.id.toolbar

    override fun setViews(view: View) {
        setupViewPager()   <----- error
    }

    private fun setupViewPager() {
        val viewPagerAdapter = ViewPagerAdapter(baseActivity.supportFragmentManager).apply {
            addFragment(FragmentE(), "E")
            addFragment(FragmentF(), "F")
        }
        viewPager.adapter = viewPagerAdapter     <----- error
        tabLayout.setupWithViewPager(viewPager)
    }
}

ViewPagerAdapter

class ViewPagerAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {

    private val titleList : MutableList<String> = ArrayList()
    private val fragmentList : MutableList<Fragment> = ArrayList()

    override fun getItem(position: Int): Fragment {
        return fragmentList[position]
    }

    override fun getCount(): Int {
        return fragmentList.size
    }

    fun addFragment(fragment: Fragment, title: String){
        titleList.add(title)
        fragmentList.add(fragment)
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return titleList[position]
    }
}

错误

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.project, PID: 20461
    java.lang.IllegalStateException: FragmentManager is already executing transactions
        at androidx.fragment.app.FragmentManager.ensureExecReady(FragmentManager.java:1778)
        at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1814)
        at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:303)
        at androidx.fragment.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:270)
        at androidx.viewpager.widget.ViewPager.setAdapter(ViewPager.java:513)
        at com.example.project.views.communication.CommunicationFragment.setupViewPager(CommunicationFragment.kt:26)
        at com.example.project.views.communication.CommunicationFragment.setViews(CommunicationFragment.kt:18)
        at com.example.project.views.base.BaseFragment.onViewCreated(BaseFragment.kt:37)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:332)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
        at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2625)
        at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2577)
        at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2722)
        at androidx.fragment.app.FragmentStateManager.activityCreated(FragmentStateManager.java:346)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1188)
        at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
        at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

也许有人对为什么会发生错误有任何想法?

我有带有3个主要片段的BottomNavigationView:A,B和C。当我尝试使用BottomNavigationView按任意顺序第二次打开FragmentC时,我看到一个错误:FragmentManager已经...

android android-fragments android-viewpager android-architecture-components android-architecture-navigation
1个回答
0
投票

使用childFragmentManager代替supportFragmentManager然后,像这样覆盖方法saveStateOverride fun saveState(): Parcelable { return null }

© www.soinside.com 2019 - 2024. All rights reserved.