亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

報錯 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

正在回答

1 回答

val?fragmentTransaction?=?supportFragmentManager.beginTransaction()?這個必須定義為局部變量,若定義為全局變量,只能提交一次。


0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

報錯 java.lang.IllegalStateException: commit already called

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號