FragmentTransaction commit 報錯:java.lang.IllegalStateException: commit already called

程序代碼如下圖所示:

在這裏插入圖片描述
橫豎屏切換時候,FragmentTransaction commit 報錯:java.lang.IllegalStateException: commit already called
錯誤信息
根據上面的錯誤信息繼續追蹤
BackStackRecord.java類裏面的 commitInternal 方法

commitInternal方法
從上面圖片源碼可以看到FragmentTransaction 調用的commit時最終是調用commitInternal 方法,在commitInternal 裏如果mCommitted是true則拋出IllegalStateException(“commit already called”);大致意思是每一個實例化後的 FragmentTransaction 的事物只能被提交一次。

解決問題方法

在mTransaction調用add或者replace方法之前,重新實例化一下 mTransaction

 mTransaction = getSupportFragmentManager().beginTransaction();//mTransaction 實例化

在這裏插入圖片描述
程序修改如上圖所示,mTransaction可以正常commit了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章