ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id...

  • 出现场景:当点击“分类”再返回“首页”时,发生error退出
 
  • BUG描述:Caused by: java.lang.IllegalArgumentException: Binary XML file line #23: Duplicate id 0x7f0d0054, tag null, or parent id 0xffffffff with another fragment for com.example.sxq123.iljmall.FragmentCatagorySpace
 
  • //framgment_home.xml
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text = "@string/home"/>

<fragment
    android:id = "@+id/fragment_space"
    android:name = "com.example.sxq123.iljmall.FragmentCatagorySpace"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

</LinearLayout>
</ScrollView>
</LinearLayout>

 

 
  • //FragmentHome.java
public class FragmentHome extends Fragment {

    private View view;
    FragmentCatagorySpace fragmentCatagorySpace ;

    @Override
    public View onCreateView(LayoutInflater inflater , ViewGroup container , Bundle savedInsataceState){
        view = inflater.inflate(R.layout.fragment_home, null);
        Log.d(this.getClass().getName(),"  onCreateView() Begin");
        initChildFragment();

        return view;
    }
    private void initChildFragment(){
        Log.d("-------------------","init space ");
        fragmentCatagorySpace = (FragmentCatagorySpace)getChildFragmentManager().findFragmentById(R.id.fragment_space);
        if(fragmentCatagorySpace != null){
            Log.d("----------------","init space success and no null");
        }
    }
}

 

 
 
  • 问题原因
  activity中不同的frament之间项目替换的时候,FragmentManager只会remove和add这些frament,
然而这些frament里面自己加载的frament(这里就是我们的FragmentCatagorySpace)是没有被remove. 很显然这是一个缺陷!
因为后一个frament(FragmentCatagorySpace)很明显是依赖与他的父frament的,应该同时递归的remove.
  那么如何解决这个问题呢!很显然就是在不用这个frament(FragmentHome)的时候把他里面加载的frament给remove掉!
这个操作在Fragment的重新onCreateView() inflate layout之前remove掉就可以解决问题了!
比如将remove的事务放在父Fragment的onDestroyView()之中执行
 
  • 修正后的代码
 
public class FragmentHome extends Fragment {

    private View view;
    FragmentCatagorySpace fragmentCatagorySpace ;

    @Override
    public View onCreateView(LayoutInflater inflater , ViewGroup container , Bundle savedInsataceState){
        view = inflater.inflate(R.layout.fragment_home, null);
        Log.d(this.getClass().getName(),"  onCreateView() Begin");
        initChildFragment();

        return view;
    }
@Override
public void onDestroyView(){
    super.onDestroyView();
    if(fragmentCatagorySpace != null){
        Log.d("-------------------", "space no null");
        FragmentManager fragmentManager = getFragmentManager();
        if(fragmentManager != null && !fragmentManager.isDestroyed()){
            final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction ();
            if(fragmentTransaction != null){
                fragmentTransaction.remove(fragmentCatagorySpace).commit();
                //commit()和commitAllowingStateLoss()都是先发送到主线程任务队列中, 当主线程准备好了再执行,异步。
                // //如果没有在inflate之前执行remove child fragmetn 的transaction,将会发生duplicate id 的exception !!!
//                fragmentTransaction.commit();//可以放在onCreateView中执行commit(),但偶尔会发生onSaveInstanceState()之后不能执行commit()的冲突
                fragmentTransaction.commitAllowingStateLoss();
                //立即执行任务队列中的transaction, 不异步 !!!!!!!!!!!!!!!重点!!!!!!!!!!!!!!!!!!!!
//防止remove事务被加到主线程任务队列中,那这样异步的话可能这些事物直到父Fragment重新执行onCreateView()
//之前都没有执行完,同样会发生duplicate id 的异常
//如果这些remove 的食物放在父Fragment的onSaveInstanceState()中执行, 由于onSaveInstanceState()调用并不
//是每一个Fragment生命周期都会被调用(????),所以偶尔也会发生duplicate id  的异常
 fragmentManager.executePendingTransactions();
                Log.d("------------------","  space destroy");
            }
        }
    }
}
    private void initChildFragment(){
        Log.d("-------------------","init space ");
        fragmentCatagorySpace = (FragmentCatagorySpace)getChildFragmentManager().findFragmentById(R.id.fragment_space);
        if(fragmentCatagorySpace != null){
            Log.d("----------------","init space success and no null");
        }
    }
}

 

 
 

转载于:https://www.cnblogs.com/s-xq/p/6159600.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值