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 ...
随机推荐
- ArcGIS_系列视频教程::精品大放送
转自:http://www.cnblogs.com/gispeng/archive/2008/11/28/1342758.html ArcGIS_系列视频教程::精品大放送 文件名: ArcG ...
- python 零散记录(四) 强调字典中的键值唯一性 字典的一些常用方法
dict中键只有在值和类型完全相同的时候才视为一个键: mydict = {1:1,':1} #此时mydict[1] 与 mydict['1']是两个不同的键值 dict的一些常用方法: clear ...
- C#的类成员初始化顺序
C#的类成员的定义和声明如下 using UnityEngine; using System.Collections; public class TestController : ECControll ...
- Python初始值表示为无穷大
之前只知道设置变量的初始值为0.今天在写网络路径分析的时候,为了找到离任意坐标距离最近的节点,初始设置最短距离为无穷大,然后不断的去替换,直到找到最近的节点. 刚开始设置是min_dis = 9999 ...
- 《University Calculus》-chape6-定积分的应用-求体积
定积分一个广泛的应用就是在求解一些“看似不规则”的几何体的体积,之所以说看似不规则,是因为不规则之下还是有一定的“规则性”可言的,我们就是需要抓住这些线索进行积分运算得到体积. 方法1:切片法. 这里 ...
- .net 码农转战 iOS - 初探
好久没写博客了,之前还打算把毕业设计中涉及到的两个算法拿出来说说(脸型分析 + 声音分析),博文都写了一半了,后来实在太忙了,那篇随笔也就沉在草稿列表中没动过. 我原先是专职 .net 开发的,在公司 ...
- linux内核中经常用到的设备初始化宏
内核使用了大量不同的宏来标记具有不同作用的函数和数据结构.如宏__init.__devinit等.这些宏在include/linux/init.h头文件中定义.编译器通过这些宏可以把代码优化放到合适的 ...
- 394. Coins in a Line
最后更新 一刷. 用数学方法是看是不是3的倍数. 不用数学方法的话要动态规划. 当前玩家,dp[i]行不行取决于dp[i-1]和dp[i-2],代表下一个玩家能不能赢,另一个玩家能赢的话当前就不能赢: ...
- kafka consumer 分区reblance算法
转载请注明原创地址 http://www.cnblogs.com/dongxiao-yang/p/6238029.html 最近需要详细研究下kafka reblance过程中分区计算的算法细节,网上 ...
- angular2自学笔记---官网项目(一)
1.单向数据绑定的'插值表达式' angular中最典型的数据显示方式:把HTML模板(template)的控件绑定到angular组件的属性(component相当于一个构造函数,下面例子中的这个构 ...