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错误的更多相关文章

  1. 安卓java.lang.IllegalStateException: The specified child already has a parent.解决方案

    在使用ViewPager的时候遇到一个错误java.lang.IllegalStateException: The specified child already has a parent. You ...

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

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

  3. 关于报错The specified child already has a parent的解决办法

    报错信息为:java.lang.IllegalStateException: The specified child already has a parent. You must call remov ...

  4. 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 ...

  5. 【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 ...

  6. 替换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); 错误 ...

  7. 怎样实现动态加入布局文件(避免 The specified child already has a parent的问题)

    首先扯点别的:我应经连续上了两个星期的班了,今天星期一.是第三个周.这个班上的也是没谁了.近期老是腰疼. 预计是累了.近期也没跑步.今天下班继续跑起. 这篇文章讲一讲怎样在一个布局文件里动态加在一个布 ...

  8. 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 ...

  9. Android IllegalStateException: The specified child already has a parent问题解决办法

    最近遇到一个很让人头疼的问题,使用viewpager动态添加页面或者删除页面时出现了问题(java.lang.IllegalStateException: The specified child al ...

随机推荐

  1. JAVA基础知识之Collections工具类

    排序操作 Collections提供以下方法对List进行排序操作 void reverse(List list):反转 void shuffle(List list),随机排序 void sort( ...

  2. SharePoint自动化系列——Add content type to list.

    转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 将创建好的content type(若是跨web application需要事先publish c ...

  3. WinFom基本属性

    常用窗体属性: AutoSroll:如果窗体内容超出了窗体范围,自动出现滚动条.默认是false. AutoSize:调节窗口大小时,保证使窗体内的内容必须全部显示出来. Location:控件左上角 ...

  4. PHP中的XML解析的5种方法

    [前言]不管是桌面软件开发,还是WEB应用,XML无处不在!然而在平时的工作中,仅仅是使用一些已经封装好的类对XML对于处理,包括生成,解析等.假期有空,于是将PHP中的几种XML解析方法总结如下: ...

  5. 电脑能上网,手机连上wifi不能上网

    电脑能上网,手机连上wifi不能上网  ,其实只要把手机的dhcp 改为我们熟悉的就行了 我此处就设置为114.114.114.114

  6. BZOJ 3226: [Sdoi2008]校门外的区间

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3226 题意:初始集合S为空.模拟四种集合操作:集合并.交.差.补集并. 思路:区间 ...

  7. web应用中web.xml配置详解

    Web.xml常用元素 <web-app> <display-name></display-name>定义了WEB应用的名字 <description> ...

  8. UVA 590 二十一 Always on the run

     Always on the run Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  9. GZFramwork快速开发框架演练之会员系统(二)添加字典模块

    开始前请先阅读 GZFramwork快速开发框架之窗体设计说明 第一步:准备模块图片 图片为2张大小分别为16x16和32x32,放在\Debug\images目录下    因为会员管理模块并不多   ...

  10. include_path详细解析

    include_path详细解析     原文地址:http://www.laruence.com/2010/05/04/1450.html 1.php默认的包含路径为 .;C:\php\pear 即 ...