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中的内存一般分成几部分?

    java中的内存被分成以下四部分: ①.代码区  ②.栈区  ③.堆区   ④.静态区域 栈区:由编译器自动分配释放,存放函数的参数值.局部变量的值等:具体方法执行结束后,系统自动释放JVM内存资源 ...

  2. 通过HtmlEmail 发送邮件

    今天第一次来上海市虹口图书馆上自习,感觉还是很爽的.自己撸代码学会了发送邮件.啥都不说了,直接撸代码吧! 首先 必须引进来三个jar包: compile 'javax.mail:mail:1.4.7' ...

  3. 【转】Tomcat调优指南

    转载地址:http://blog.csdn.net/woohooli/article/details/3954792 1          概述 本文档主要介绍了Tomcat的性能调优的原理和方法.可 ...

  4. JAVA基础知识之多线程——线程的生命周期(状态)

    线程有五个状态,分别是新建(New).就绪(Runnable).运行(Running).阻塞(Blocked)和死亡(Dead). 新建和就绪 程序使用new会新建一个线程,new出的对象跟普通对象一 ...

  5. gulp 建立一个简单的自动化

    前端项目需要的功能: 1.图片(压缩图片支持jpg.png.gif) 2.样式 (支持sass 同时支持合并.压缩.重命名) 3.javascript (检查.合并.压缩.重命名) 4.html (压 ...

  6. AIM Tech Round 3 (Div. 2) B

    Description Vasya takes part in the orienteering competition. There are n checkpoints located along ...

  7. 2016年6月26日 星期日 --出埃及记 Exodus 14:23

    2016年6月26日 星期日 --出埃及记 Exodus 14:23 The Egyptians pursued them, and all Pharaoh's horses and chariots ...

  8. Winform容器标签 打印标签 对话框控件

    一.容器标签 布局: Anchor:锁定位置,指定与窗口容器的边缘位置,会随着窗口大小的改变而改变: Dock:填充窗口的位置.一般与容器标签同时使用. 1.Panel:对控件进行分组.可以独立布局, ...

  9. 快速编译system.img、userdata.img、boot.img的方法

    快速编译system.img和boot.img的方法 快速编译system.img,可以使用这个命令: #make systemimage 快速编译boot.img,可以使用以下命令: #make b ...

  10. PostGIS_导入shp格式的数据

    1.导入shp到PostGIS 开启PostGIS shp Loader,如下图: 打开View connection details....输入连接数据库的参数,如下: 连接成功后,点击Add Fi ...