我的android异常系列——java.lang.IllegalStateException: commit already called
2017-07-21 17:09
417 查看
我的异常系列目录为:http://www.jianshu.com/p/cb10697226ef
出现commit already called这个异常的原因是:同一个FragmentTransaction只能commit一次(调用commit方法),可在它的实现类 BackStackRecord中找到以下代码,每一个BackStackRecord对象都会维护一个布尔变量(mCommitted),当commit后赋值这个变量为true,下次再commit时会检查这个变量,如果是true,则抛出以上异常。commit方法的源码如下:
综上,我们在使用FragmentTransaction时,需要保证这个对象只调用一次commit,下次commit时需要重新获取!
出现commit already called这个异常的原因是:同一个FragmentTransaction只能commit一次(调用commit方法),可在它的实现类 BackStackRecord中找到以下代码,每一个BackStackRecord对象都会维护一个布尔变量(mCommitted),当commit后赋值这个变量为true,下次再commit时会检查这个变量,如果是true,则抛出以上异常。commit方法的源码如下:
```java @Override public int commit() { return commitInternal(false); } int commitInternal(boolean allowStateLoss) { if (mCommitted) throw new IllegalStateException("commit already called"); if (FragmentManagerImpl.DEBUG) { Log.v(TAG, "Commit: " + this); LogWriter logw = new LogWriter(TAG); PrintWriter pw = new PrintWriter(logw); dump(" ", null, pw, null); } mCommitted = true; //这里赋值为true了 if (mAddToBackStack) { mIndex = mManager.allocBackStackIndex(this); } else { mIndex = -1; } mManager.enqueueAction(this, allowStateLoss); return mIndex; } ```
综上,我们在使用FragmentTransaction时,需要保证这个对象只调用一次commit,下次commit时需要重新获取!
相关文章推荐
- Android:java.lang.IllegalStateException: commit already called
- 异常处理---java.lang.IllegalStateException: getWriter() has already been called for this
- java.lang.IllegalStateException: commit already called
- java.lang.IllegalStateException: commit already called 的处理办法
- android异常:java.lang.IllegalStateException: The specified child already has a parent. You must call r
- 验证码异常:java.lang.IllegalStateException: getOutputStream() has already been called for this response
- java.lang.IllegalStateException: commit already called
- struts2 java.lang.IllegalStateException: getWriter() has already been called for this response(异常解决)
- java.lang.IllegalStateException: commit already called
- SSH中发生异常java.lang.IllegalStateException: getOutputStream() has already been called for this response
- Android异常分析之 java.lang.IllegalStateException:the specified child already has a parent.
- Java.lang.IllegalStateException: commit already called
- Android 异常 java.lang.RuntimeException: Can't create handler inside thread that has not called Looper
- java.lang.IllegalStateException: commit already called详细解决办法
- java.lang.IllegalStateException: commit already called
- java.lang.IllegalStateException: commit already called
- java.lang.IllegalStateException: commit already called
- java.lang.IllegalStateException: commit already called
- java.lang.IllegalStateException: getOutputStream() has already been called for this response 分类: 异常
- 下载文件时报异常:java.lang.IllegalStateException: getOutputStream() has already been called for this respons