報錯 java.lang.IllegalStateException: commit already called
????private?fun?switchFragment(selectIndex:?Int)?{ ????????if?(tab1Fragment?==?null)?{ ????????????tab1Fragment?=?StudyFragment() ????????????//???????????添加參數(shù) ????????????addBundleToFragment(tab1Fragment!!,?"tab1") ????????} ????????if?(tab2Fragment?==?null)?{ ????????????tab2Fragment?=?StudyFragment() ????????????//????????????添加參數(shù) ????????????addBundleToFragment(tab2Fragment!!,?"tab2") ????????} ????????if?(tab3Fragment?==?null)?{ ????????????tab3Fragment?=?StudyFragment() ????????????//????????????添加參數(shù) ????????????addBundleToFragment(tab3Fragment!!,?"tab3") ????????} //獲取fragment對象 ????????val?fragment:?Fragment??=?when?(selectIndex)?{ ????????????0?->?{ ????????????????tab1Fragment ????????????} ????????????1?->?{ ????????????????tab2Fragment ????????????} ????????????2?->?{ ????????????????tab3Fragment ????????????} ????????????else?->?{ ????????????????throw?IllegalStateException("${selectIndex}下標(biāo)不符合預(yù)期") ????????????} ????????}??:?return????????//如果fragment為空,則返回 //????????切換fragment顯示 ????????if?(fragment?!=?null)?{ ????????????if?(!fragment.isAdded)?{ ????????????????fragmentTransaction.add(R.id.fragment_container,?fragment)//添加fragment ????????????} ????????????fragmentTransaction.show(fragment)//顯示fragment ????????} ????????//???隱藏之前顯示的shownFragment,以多個Fragemnt重疊 ????????if?(shownFragment?!=?null)?{ ????????????fragmentTransaction.hide(shownFragment!!) ????????} ????????shownFragment?=?fragment//設(shè)置當(dāng)前顯示的為fragment ????????fragmentTransaction.commitAllowingStateLoss()//提交業(yè)務(wù) ????}
報錯指向這一行:
????fragmentTransaction.commitAllowingStateLoss()//提交業(yè)務(wù)
整個文件只有這一個提交業(yè)務(wù) ,為什么說已經(jīng)called
2021-07-21