bug_ _ _fragment的??
============ 3 java.lang.IllegalStateException: Fragment(XXFragment) not attached to Activity异常。
之前增加一个判断isAdded(),
两外说这个异常解决办法的有http://stackoverflow.com/questions/6870325/android-compatibility-package-fragment-not-attached-to-activity。 这个是针对另外一种情况下的解决方式。
2、在使用Fragment保存参数的时候,可能是因为需要保存的参数比较大或者比较多,这种情况下页会引起异常。比如代码
Bundle b = new Bundle();
b.putParcelable("bitmap", bitmap2);
imageRecognitionFragment.setArguments(b);
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
异常。============ 3 java.lang.IllegalStateException: Fragment bb{42261900} not attached to Activity
A.处理异常java.lang.IllegalStateException: Fragment bb{42261900} not attached to Activity
处理方式:由于在线程中调用Fragment以下方法会出现fragment已经没有附加到activity,
所以在调用这些方法时候加isAdded()判断
Fragment源码
--------------------1-----------------------------
public final Resources getResources()
{
if (this.mActivity == null) {
throw new IllegalStateException("Fragment " + this + " not attached to Activity");
}
return this.mActivity.getResources();
}
public final CharSequence getText(int resId)
{
return getResources().getText(resId);
}
public final String getString(int resId)
{
return getResources().getString(resId);
}
public final String getString(int resId, Object[] formatArgs)
{
return getResources().getString(resId, formatArgs);
}
-----------------2--------------------
public LoaderManager getLoaderManager()
{
if (this.mLoaderManager != null) {
return this.mLoaderManager;
}
if (this.mActivity == null) {
throw new IllegalStateException("Fragment " + this + " not attached to Activity");
}
this.mCheckedForLoaderManager = true;
this.mLoaderManager = this.mActivity.getLoaderManager(this.mWho, this.mLoadersStarted, true);
return this.mLoaderManager;
}
-----------------3--------------------
public void startActivity(Intent intent)
{
if (this.mActivity == null) {
throw new IllegalStateException("Fragment " + this + " not attached to Activity");
}
this.mActivity.startActivityFromFragment(this, intent, -1);
}
-----------------4--------------------
public void startActivityForResult(Intent intent, int requestCode)
{
if (this.mActivity == null) {
throw new IllegalStateException("Fragment " + this + " not attached to Activity");
}
this.mActivity.startActivityFromFragment(this, intent, requestCode);
}
============ 3 Fragment抛出java.lang.IllegalStateException: Fragment not attached to Activity?
java.lang.IllegalStateException: Fragment not attached to Activity : 用 isAdded()判断是否调用资源
异常分析
关于Fragment(XXFragment) not attached to Activity异常,是因为Fragment的还没有Attach到Activity时,调用了如getResource()等,需要上下文Content的函数。
解决办法
调用资源之前判断Fragment是否Attach到Activity,使用isAdded()方法。
isAdded() : //Return true if the fragment is currently added to its activity.
if(isAdded()){
getResources().getString(R.string.app_name);
}
============2 解决IllegalStateException: Can not perform this action after onSaveInstanceState
今天使用Fragment的时候,出现了这个错误 IllegalStateException: Can not perform this action after onSaveInstanceState:
- E/AndroidRuntime(12747): Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1314)
- at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1325)
是在使用FragmentTransition的 commit方法添加一个Fragment的时候出现的,后来在官网找到了相关的
public abstract int commitAllowingStateLoss ()
Added in API level 11
Like commit()
but allows the commit to be executed after an activity's state is saved. This is dangerous
because the commit can be lost if the activity needs to later be restored from its state, so this should
only be used for cases where it is okay for the UI state to change unexpectedly on the user.
大致意思是说我使用的 commit方法是在Activity的onSaveInstanceState()之后调用的,这样会出错,
因为onSaveInstanceState方法是在该Activity即将被销毁前调用,来保存Activity数据的,如果在保存玩状态后再给它添加Fragment就会出错。解决办法就是把commit()方法替换成 commitAllowingStateLoss()就行了,其效果是一样的。
========1 java.lang.IllegalStateException: Fragment already added异常的处理
当快速双击调用FragmentTransaction.add()方法添加fragmentA,而fragmentA不是每次单独生成的,就会引起这个异常。
DialogFragment.show()内部调用了FragmentTransaction.add()方法,所以调用DialogFragment.show()方法时候也可能会出现这个异常。
在add()方法时候,先判断 fragmentA.isAdded(),如下调用可以避免该异常:
if(!fragmentA.isAdded()){
FragmentManager manager = ((FragmentActivity)context).getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.add(fragmentA, "fragment_name");
ft.commit();
}