報錯 java.lang.IllegalStateException: commit already called
????private?fun?switchFragment(selectIndex:?Int)?{
????????if?(tab1Fragment?==?null)?{
????????????tab1Fragment?=?StudyFragment()
????????????//???????????添加參數
????????????addBundleToFragment(tab1Fragment!!,?"tab1")
????????}
????????if?(tab2Fragment?==?null)?{
????????????tab2Fragment?=?StudyFragment()
????????????//????????????添加參數
????????????addBundleToFragment(tab2Fragment!!,?"tab2")
????????}
????????if?(tab3Fragment?==?null)?{
????????????tab3Fragment?=?StudyFragment()
????????????//????????????添加參數
????????????addBundleToFragment(tab3Fragment!!,?"tab3")
????????}
//獲取fragment對象
????????val?fragment:?Fragment??=?when?(selectIndex)?{
????????????0?->?{
????????????????tab1Fragment
????????????}
????????????1?->?{
????????????????tab2Fragment
????????????}
????????????2?->?{
????????????????tab3Fragment
????????????}
????????????else?->?{
????????????????throw?IllegalStateException("${selectIndex}下標不符合預期")
????????????}
????????}??:?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//設置當前顯示的為fragment
????????fragmentTransaction.commitAllowingStateLoss()//提交業務
????}報錯指向這一行:
????fragmentTransaction.commitAllowingStateLoss()//提交業務
整個文件只有這一個提交業務 ,為什么說已經called
2021-07-21