1 Caused by: java.lang.IllegalArgumentException: Binary XML file line #25: Duplicate id 0x7f070193, tag null, or parent
id 0x0 with another fragment for xxxx
2 有时会出现Fragment重复或者覆盖到一起,这是因为当屏幕发生旋转,Activity发生重新启动,默认的Activity中的Fragment也会跟着Activity重新创建;这样造成当旋转的时候,本身存在的Fragment会重新启动,然后当执行Activity的onCreate时,又会再次实例化一个新的Fragment,这就是出现的原因。
解决方法:
这种异常是嵌套片段(nested fragment)与系统版本不相容导致的,而嵌套片段通常支持Android4.2。一个fragment的UI中嵌套另一个fragment,极有可能会造成程序运作异常。
注:嵌套片段只能在动态添加的操作中完成,也就是说,当layout中已经包含了一个<fragment>,就不能将再将这个layout嵌入到fragment中.private static View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.map, container, false);
} catch (InflateException e) {
/* map is already there, just return view as it is */
}
return view;
}
2 有时会出现Fragment重复或者覆盖到一起,这是因为当屏幕发生旋转,Activity发生重新启动,默认的Activity中的Fragment也会跟着Activity重新创建;这样造成当旋转的时候,本身存在的Fragment会重新启动,然后当执行Activity的onCreate时,又会再次实例化一个新的Fragment,这就是出现的原因。
那么如何解决呢:
其实通过检查onCreate的参数Bundle savedInstanceState就可以判断,当前是否发生Activity的重新创建:
默认的savedInstanceState会存储一些数据,包括Fragment的实例,所以在Activity的onCreate中:
if(savedInstanceState == null)
{
mFOne = new FragmentOne();
FragmentManager fm = getFragmentManager();
FragmentTransaction tx = fm.beginTransaction();
tx.add(R.id.id_content, mFOne, "ONE");
tx.commit();
}