• 出现场景:当点击“分类”再返回“首页”时,发生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");
}
}
}
 
 

ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id的更多相关文章

  1. fragment显示 Binary XML file line #12: Error inflating class fragment 错误

    问题 最近换了新机子,今天在静态用fragment时突然发现闪退,一看显示 Binary XML file line #12: Error inflating class fragment 错误 后面 ...

  2. 【Android开发实践】android.view.InflateException: Binary XML file line #12: Error inflating class fragment问题解决

    一般出现的原因是fragment引入的包错了,应该是import android.app.ListFragment;而不是import android.support.v4.app.ListFragm ...

  3. Android 中关于Fragment嵌套Fragment的问题

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/5802146.html 问题描述: 在项目中Activity A中嵌套Fragment B,Fragment ...

  4. ionic3构建过程中遇到的找不到AndroidManifest.xml的问题

    问题如下: Failed to install 'ionic-plugin-keyboard': Error: ENOENT: no such file or directory, open '/Us ...

  5. 上传jar包到maven中央仓库过程中遇到的一些问题总结!

    网上有很多相关教程, 我按步骤一步步走下来, 都还算顺利, 简单列举一下步骤以及其中需要注意的几个点(不详细, 不适合当教程) 第一步: 到https://issues.sonatype.org/se ...

  6. Jenkins初级使用过程中的异常处理(1)

    在使用Jenkins一些基本功能的时候,或者说是基本插件的时候,会遇到各种各样的报错.这里就设想模拟一下,重现一下以前遇到过的问题,记录一下.虽说是Jenkins使用过程中出现这样的问题,但实际上可以 ...

  7. springmvc配置过程中遇到的一些问题总结

    springmvc配置过程中遇到的一些问题总结 1.配置tomcat过程中的错误: 2.配置web.xml中DispatchServlet报红(配置好已有依赖条件下) 解决的办法: 因为新添加依赖,m ...

  8. activity 嵌套一级fragment,一级fragment嵌套二级fragment,在一级fragment中刷新二级fragment中的UI

    今天遇到挺纠结的问题,由于产品设计的问题,技术上涉及到activity 嵌套一级fragment,一级fragment嵌套二级fragment,在一级fragment中刷新二级fragment中的UI ...

  9. Bulid过程中中遇到的问题UnityEditor.BuildPlayerWindow+BuildMethodException: '' is an incorrect path for a scene file. BuildPlayer expects paths relative to the project folder.

    今天,在Bulid的过程中,遇到了一个错误“ UnityEditor.BuildPlayerWindow+BuildMethodException: '' is an incorrect path f ...

随机推荐

  1. [BOT] 一种android中实现“圆角矩形”的方法

    内容简介 文章介绍ImageView(方法也可以应用到其它View)圆角矩形(包括圆形)的一种实现方式,四个角可以分别指定为圆角.思路是利用"Xfermode + Path"来进行 ...

  2. ImageView缩放选项

    ImageView.ScaleType 将图片边界缩放到所在view边界时的缩放选项. Options for scaling the bounds of an image to the bounds ...

  3. Java 征途:行者的地图

    前段时间应因缘梳理了下自己的 Java 知识体系, 成文一篇望能帮到即将走进或正在 Java 世界跋涉的程序员们. 第一张,基础图 大约在 2003 年我开始知道 Java 的(当时还在用 Delph ...

  4. Android消息传递之基于RxJava实现一个EventBus - RxBus

    前言: 上篇文章学习了Android事件总线管理开源框架EventBus,EventBus的出现大大降低了开发成本以及开发难度,今天我们就利用目前大红大紫的RxJava来实现一下类似EventBus事 ...

  5. SELECT INTO 和 INSERT INTO SELECT 两种表复制语句

    Insert是T-sql中常用语句,Insert INTO table(field1,field2,...) values(value1,value2,...)这种形式的在应用程序开发中必不可少.但我 ...

  6. CRL快速开发框架系列教程十三(嵌套查询)

    本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...

  7. 挑子学习笔记:特征选择——基于假设检验的Filter方法

    转载请标明出处: http://www.cnblogs.com/tiaozistudy/p/hypothesis_testing_based_feature_selection.html Filter ...

  8. peer not authenticated的终极解决方案

    一.前述 使用httpclient发起https请求时,可能会遇到如下异常: javax.net.ssl.SSLPeerUnverifiedException: peer not authentica ...

  9. C# 程序中嵌入百度地图

    本例是对WinForm中使用百度地图的简要介绍.百度地图目前支持Android开发,IOS开发,Web开发,服务接口,具体可以参照'百度地图开放平台'. [动态加载百度地图]涉及到的知识点: WebB ...

  10. maven 中snapshot版本和release版本的区别

    maven中的仓库分为两种,snapshot快照仓库和release发布仓库.snapshot快照仓库用于保存开发过程中的不稳定版本,release正式仓库则是用来保存稳定的发行版本.定义一个组件/模 ...