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 ...
随机推荐
- mybatis+springMVC新感悟
一直以为按照例子里写的.先编写User实体类,之后在编写User.xml之后在配置文件里指明接口文件.然后在controller中就可以通过就可以通过定义接口,在取值 IUserOperation u ...
- 20150608_Andriod 发布问题处理
参考地址: http://blog.csdn.net/cxc19890214/article/details/39120415 问题:当我们开发完成一个Android应用程序后,在发布该应用程序之前必 ...
- Balance
Balance Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11947 Accepted: 7464 Description ...
- Animator组件关闭再打开后参数丢失问题
问题如下,因为再激活Animator时,它会重置一次,参数也会丢失 这个问题一直存在,论坛给出的解释是把参数缓存下来,在激活时重置 http://answers.unity3d.com/questio ...
- UVa(247),Floyd做传递闭包
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- #ifdef __cplusplus extern "C" { #endif
1.在好多程序中我们会遇到下面代码段 #ifdef __cplusplus extern "C" { #endif //c语法代码段 #ifdef __ ...
- 基于busybox和LFS的linux系统定制
自从在大学知道了Linux这玩意是可以定制的之后,一直想做出一版属于自己的Linux系统.最近工作比较闲,终于塌下心来好好学习了一下. 目前来说,我接触的定制Linux的方法主要有两种: 1. ...
- xcode下编译.a文件的路径
http://www.it165.net/pro/html/201503/36842.html 每当我们编译之后, 实际上系统就给我编译好了一个可以运行的.app文件,在某个路径下 如果我们建立的是静 ...
- 实现Action类
实现Action类 1.Action类的作用: (1)封装HTTP的请求参数: (2)处理用户请求: (3)封装处理结果. 2.Action类是什么,在Action类中应该包含什么: Action类就 ...
- Oracle中如何使用REGEXP_SUBSTR函数
REGEXP_SUBSTR函数格式如下: function REGEXP_SUBSTR(String, pattern, position, occurrence, modifier) __srcst ...