Android-The specified child already has a parent. You must call removeView() on the child's parent first.
这个问题搞了我半天了,网上有很多人说需要找到该控件的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.的更多相关文章
- 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 ...
- 【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 ...
- 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 ...
- 替换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); 错误 ...
- 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, ...
- Call removeView() on the child's parent first
extends:http://stackoverflow.com/questions/6526874/call-removeview-on-the-childs-parent-first Except ...
- 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 ...
- bug_ _fragment_“The specified child already has a parent. You must call removeView"的解决以及产生的原因
这个异常的出现往往是因为非法使用了某些方法引起的. 从字面意思上是说这个特定的child已经有一个parent了,你必须在这个parent中首先调用removeView()方法,才能继续你的内容.这里 ...
- 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 ...
随机推荐
- ☀【CSS3】背景图片
CSS3全新的背景图片方案http://www.cnblogs.com/rubylouvre/p/3401125.html
- SharePoint 2010 母版页定制小思路介绍
转:http://tech.ddvip.com/2013-11/1384521515206064.html 介绍:我们使用SharePoint2010做门户网站,经常需要定制母版页,但是2010提供的 ...
- Authorized users only. All activity may be monitored and reported.
Authorized users only. All activity may be monitored and reported. Directory: /home/oracle 如出现如上问题,是 ...
- 搭建Git远程仓库(也就是Git服务器,不用再连Github了)
github上一般托管的代码都是公开的,任何人都可以查看.复制下载等,而私有的项目则需要付费.所以可以自己搭建一个git服务,自己人用. 首先安装git: sudo apt-get install g ...
- python多进程的理解 multiprocessing Process join run
最近看了下多进程. 一种接近底层的实现方法是使用 os.fork()方法,fork出子进程.但是这样做事有局限性的.比如windows的os模块里面没有 fork() 方法. windows:.lin ...
- C#4.0中var和dynamic的区别
1. var表示“变量的类型是在编译时决定的”, var让你在初始化变量时少输入一些字,编译器会根据右值来推断出变量的类型, var只能用于局部变量的定义,你不能把类的属性定义成 var,也不能把方法 ...
- 怎样在Yii中显示静态页
在web应用中,我们经产更需要显示静态页,如“关于我们”等,这些文件通常是静态页,通常有如下几种处理方法: 1.把独立的html文件存在Web服务器能直接访问的目录下.这种方案的缺点是很难维护网页布局 ...
- DiscreteSeekBar---->SeekBar的使用
build: compile 'org.adw.library:discrete-seekbar:1.0.0' 在布局中的使用: <org.adw.library.widgets.discret ...
- ASP.NET多线程下使用HttpContext.Current为null解决方案
多线程或者异步调用中如何访问HttpContext? 前面我还提到在APM模式下的异步完成回调时,访问HttpContext.Current也会返回null,那么此时该怎么办呢? 答案有二种:1. 在 ...
- UVA 297 Quadtrees(四叉树建树、合并与遍历)
<span style="font-size: 18pt; font-family: Arial, Helvetica, sans-serif; background-color: r ...