这个问题搞了我半天了,网上有很多人说需要找到该控件的parent后,让该parent 先remove需要添加的控件,然后在添加,如:

if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null) {
parent.removeView(view);
}
}
try {
view = inflater.inflate(R.layout.fragment_main, container, false);
} catch (InflateException e) { }

也很多人说将inflater.inflate(R.layout.change_layout)改为 inflater.inflate(R.layout.change_layout, container, false);

但是我的情况是这样的,有一个FragmentManager,控制4个Fragment,其中,3个Fragment是直接从layout文件获取,所以直接调用inflater.inflate(R.layout.change_layout, container, false);就可以,但是另一个Fragment是动态从我的自定义的View类加载,我一开始总是使用addView,不管我怎么通过父类去remove,都报一样的错.

后来发现,在Fragment的onCreateView方法中,不用显示addView方法,只要实例化一个View对象,即可(甚至不用返回该View对象).如

	@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
frameLayout = (FrameLayout) getActivity().findViewById(R.id.content);
final String title = "最近一周AQI变化图";
data = new int[] { 1, 2, 3, 4, 5, 6, 7 };
xLables = new String[] { "一", "二", "三", "四", "五", "六", "七" }; view_brokenline = new BrokenLineView(frameLayout.getContext(), title,
xLables, null, data);
return null;
}

Android-The specified child already has a parent. You must call removeView() on the child's parent first.的更多相关文章

  1. android 异常信息The specified child already has a parent. You must call removeView() on the child's parent first. 的处理方法

    [Android异常信息]: The specified child already has a parent. You must call removeView() on the child's p ...

  2. 【Android异常】The specified child already has a parent. You must call removeView() on the child's parent first.

    错误信息: Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must ...

  3. java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

    在ViewPager中,用Fragment显示页面时,报错: java.lang.IllegalStateException: The specified child already has a pa ...

  4. 替换Fragment 报错 The specified child already has a parent. You must call removeView() on the child's parent first.

    在将一个fragment替换到一个frameLayout的时候报错: code: transaction.replace(R.id.fragment_container, fragment2); 错误 ...

  5. Fragment+FragmentActivity出现The specified child already has a parent. You must call removeView() on the child's parent first.

    这个异常是出现在Fragment中的onCreateView方法中初始化布局时发生的. View view = inflater.inflate(R.layout.fragment3_layout, ...

  6. Call removeView() on the child's parent first

    extends:http://stackoverflow.com/questions/6526874/call-removeview-on-the-childs-parent-first Except ...

  7. java.lang.IllegalStateException: The specified child already has a parent. You must call removeView

     java.lang.IllegalStateException: The specified child already has a parent. You must call removeVi ...

  8. bug_ _fragment_“The specified child already has a parent. You must call removeView"的解决以及产生的原因

    这个异常的出现往往是因为非法使用了某些方法引起的. 从字面意思上是说这个特定的child已经有一个parent了,你必须在这个parent中首先调用removeView()方法,才能继续你的内容.这里 ...

  9. fragment The specified child already has a parent. You must call removeView()

    在切换Fragment的时候出现:The specified child already has a parent. You must call removeView()异常. 错误主要出在Fragm ...

随机推荐

  1. bzoj1034

    贪心 尽可能让最强的赢,最弱的赢,都不行则最弱打最强 感性的想,我肯定要尽可能的赢,而且赢的要对等 实在不能赢就拿最小的拼,所谓的田忌赛马策略 由于总分一定,己方最差即己方最好时对方的分数 ..] o ...

  2. 20140708郑州培训第二题Impossible Game

    Impossible Game题目描述你发明了一个简单的单人电脑游戏.在开始游戏时,玩家必须输入一个长度为 K 的字符串,且这个字符串的元素只能为‘A’‘B’‘C’或者‘D’.每一种字符串都代表一种颜 ...

  3. HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  4. JavaScript---网络编程(4)-Date、Math、Global和自定义对象

    本节博客进行Date对象.Math对象.Global对象和自定义对象的用法演示. 首先是一个out.js文件内容(输出到网页中的自定义输出方法): function println(parma){ d ...

  5. 好用的ajax后台框架

    dwz 简单实用的国产jquery Ui框架 http://www.j-ui.com/#_blank

  6. mv、umask、chattr、lsattr命令

    mv命令行,即move 将文件移动到目录下 对文件或目录重命名 umask chattr 设置文件或目录的隐藏属性 lsattr显示文件或目录的隐藏属性 ls mv 1.txt aa ls cd aa ...

  7. SQL Server Reporting Services 自定义数据处理扩展DPE(Data Processing Extension)

    最近在做SSRS项目时,遇到这么一个情形:该项目有多个数据库,每个数据库都在不同的服务器,但每个数据库所拥有的数据库对象(table/view/SPs/functions)都是一模一样的,后来结合网络 ...

  8. Android下的Linux指令集

    Android目录结构 data app:用户安装的应用 data:应用的专属文件夹 system:系统的配置信息,注册表文件 anr:anr异常的记录信息 dev:devices的缩写 存放设备所对 ...

  9. Zend Framework 2参考Zend\Authentication(摘要式身份验证)

    Zend Framework 2参考Zend\Authentication(摘要式身份验证) 介绍 摘要式身份验证是HTTP身份验证的方法,提高了基本身份验证时提供的方式进行身份验证,而无需在网络上以 ...

  10. Pgsql数据库jsonb操作函数集合

    CREATE OR REPLACE FUNCTION "json_object_del_path"( "json" json, "key_path&q ...