The specified child already has a parent错误
10-05 23:39:48.187: E/AndroidRuntime(12854): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
10-05 23:39:48.187: E/AndroidRuntime(12854): at android.view.ViewGroup.addViewInner(ViewGroup.java:1976)
10-05 23:39:48.187: E/AndroidRuntime(12854): at android.view.ViewGroup.addView(ViewGroup.java:1871)
10-05 23:39:48.187: E/AndroidRuntime(12854): at android.view.ViewGroup.addView(ViewGroup.java:1828)
10-05 23:39:48.187: E/AndroidRuntime(12854): at android.view.ViewGroup.addView(ViewGroup.java:1808)
10-05 23:39:48.187: E/AndroidRuntime(12854): at com.solar.WaterLevelActivity.InitUnitTimeLowestWaterLevel(WaterLevelActivity.java:49)
10-05 23:39:48.187: E/AndroidRuntime(12854): at com.solar.WaterLevelActivity.InitView(WaterLevelActivity.java:30)
10-05 23:39:48.187: E/AndroidRuntime(12854): at com.solar.WaterLevelActivity.onCreate(WaterLevelActivity.java:25)
10-05 23:39:48.187: E/AndroidRuntime(12854): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-05 23:39:48.187: E/AndroidRuntime(12854): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-05 23:39:48.187: E/AndroidRuntime(12854): ... 11 more
补充说明:InitUnitTimeLowestWaterLevel()在OnCreate函数中被调用,用来初始化界面。实现的效果是动态添加界面。
下面是出错的代码:

1 private void InitUnitTimeLowestWaterLevel() {
2 LinearLayout listMachines = (LinearLayout) findViewById(R.id.water_level_page_water_level_setting);
3 LayoutInflater inflater = LayoutInflater.from(WaterLevelActivity.this);
4 View tlWaterLevelInfo = inflater.inflate(R.layout.unit_time_water_level_item, null);
5 listMachines.addView(tlWaterLevelInfo);
6 listMachines.addView(tlWaterLevelInfo); //这个地方出错了。
7 }

错误的原因:通过LayoutInflater实例化的一个布局(View)。被重复添加了两次。而一个View只能有一个父控件。当第二次添加的时候,又要改变这个控件的父控件(虽然是同一个父控件,但是也要通过改变该View的父控件来实现)。运行时又不允许一个有父控件的子控件在有了一个父控件时,改变他的父控件。
解决方案:重新用布局文件实例化一个子控件,再添加到父控件上。
The specified child already has a parent错误的更多相关文章
- 安卓java.lang.IllegalStateException: The specified child already has a parent.解决方案
在使用ViewPager的时候遇到一个错误java.lang.IllegalStateException: The specified child already has a parent. You ...
- bug_ _fragment_“The specified child already has a parent. You must call removeView"的解决以及产生的原因
这个异常的出现往往是因为非法使用了某些方法引起的. 从字面意思上是说这个特定的child已经有一个parent了,你必须在这个parent中首先调用removeView()方法,才能继续你的内容.这里 ...
- 关于报错The specified child already has a parent的解决办法
报错信息为:java.lang.IllegalStateException: The specified child already has a parent. You must call remov ...
- 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 ...
- 【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 ...
- 替换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); 错误 ...
- 怎样实现动态加入布局文件(避免 The specified child already has a parent的问题)
首先扯点别的:我应经连续上了两个星期的班了,今天星期一.是第三个周.这个班上的也是没谁了.近期老是腰疼. 预计是累了.近期也没跑步.今天下班继续跑起. 这篇文章讲一讲怎样在一个布局文件里动态加在一个布 ...
- 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 ...
- Android IllegalStateException: The specified child already has a parent问题解决办法
最近遇到一个很让人头疼的问题,使用viewpager动态添加页面或者删除页面时出现了问题(java.lang.IllegalStateException: The specified child al ...
随机推荐
- YTU 3020: 对称矩阵(数组)
3020: 对称矩阵(数组) 时间限制: 1 Sec 内存限制: 128 MB 提交: 3 解决: 2 题目描述 已知A和B为两个n*n阶的对称矩阵,输入时,对称矩阵只输入下三角行元素,存入一维数 ...
- IIS装好后,局域网不能访问
IIS默认安装完成后,可以通过http:\\localhost或http:\\127.0.0.1或者http:\\ip地址访问,但是局域网内却不能通过IP来访问,通常只需要将防火墙关掉就好了,也在在防 ...
- Ugly Numbers
Ugly Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21918 Accepted: 9788 Descrip ...
- opencv通过dll调用matlab函数,图片作为参数
[blog 项目实战派]opencv通过dll调用matlab函数,图片作为参数 前文介绍了如何“csharp通过dll调用opencv函数,图片作为参数”.而在实 ...
- 如何用Pr完成作业~
要求~ 我的工具~(随便搞搞就好了,自己的录音还没弄~)
- Myeclipse 添加server library
来自网络资料 (1)File->New->Other (2)弹出窗口勾上Show All Wizards,然后在type fiter text那里输入Server,选中server-> ...
- iOS- 详解文本属性Attributes
1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSFontAttributeName : [UIFont systemFontOfSize:_fontS ...
- C语言培训第一天
下面是一些命令,先来谈谈今天的若干收获吧! 计算机中的一切文件都是以二进制补码的形式存在,问题也就来了. 第一个问题 如果我们给一个无符号的数赋值一个负数,他会读取到什么,又会输出什么?(似乎问题和上 ...
- SpringMVC拦截器2(资源和权限管理)(作为补充说明)
SpringMVC拦截器(资源和权限管理) 1.DispatcherServlet SpringMVC具有统一的入口DispatcherServlet,所有的请求都通过DispatcherServle ...
- 4.给定一个正整数m,统计m的位数,分别打印每一位数字,再按照逆序打印出各位数字。 要求:m定义为类的属性,需定义构造函数为m赋值;当m大于99999时,输出错误信息“the number is too large”,不再执行。
package a; public class ShuZi { int m; public int getM() { return m; } public void setM(int m) { thi ...