java.lang.IllegalStateException: commit already called
程序员文章站
2024-01-13 15:31:46
...
项目中的某个模块需要用到底部导航栏和上面的几个Fragment切换逻辑,写完之后运行的时候报错:
java.lang.IllegalStateException: commit already called
原因是我将FragmentTransaction 作为全局变量使用,而FragmentTransaction transaction的机制是只能commit一次,虽然我在类中有很多方法都用到了FragmentTransaction。
但是究其本源,都是在一个名为showFragment的方法中用到的,其他方法只是我用来减少重复代码的。
所以解决方法就是在根方法中将FragmentTransaction定义为局部变量,其他方法调用的时候,将其作为实参传进去就好了。
部分代码:
private void showFragment(int index){//根方法
FragmentTransaction transaction = mFragmentManager.beginTransaction();
switch (index){
case 0:
if(mWholeFragment != null && !mWholeFragment.isAdded()){
ActivityUtils.addFragmentToActivityWithTag(
mFragmentManager
,mWholeFragment,
R.id.rl_vote_container,TAG_WHOLE_FRAGMENT);
}
//其他方法调用
hideAllFragment(transaction);
transaction.show(mWholeFragment);
break;
注:FragmentManager是可以定义为全局的
推荐阅读
-
java.lang.IllegalStateException: commit already called
-
tomcat6下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法
-
Zuul Filter过滤器返回信息提示getWriter() has already been called for this response
-
解决java.lang.IllegalStateException: getWriter() has already been called for this response
-
java.lang.IllegalStateException: getOutputStream() has already been called for this response
-
getOutputStream() has already been called for this response解决方案
-
getOutputStream() has already been called for this response解决方案
-
getOutputStream() has already been called for this response
-
tomcat6下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法
-
tomcat6下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法