java.lang.IllegalStateException The specified child already has a parent. You must call removeView()
来源:互联网 发布:软件正版化领导小组 编辑:程序博客网 时间:2024/05/16 15:43
在android开发过程中,有时会在不同情况下遇到同种问题:
- java.lang.IllegalStateException The specified child already has a parent. You must call removeView() on the child's parent first.
新建一个项目,布局文件中仅包含一个TextView和一个ImageView,布局方式是线性布局(具体可以参考后面的源代码),运行的结果就是显示一个文本和一张图片,
在Activity的onCreate()方法中,我们通常使用以下这种方式来使用布局文件main.xml。
- setContentView(R.layout.main);
这里为了解释今天要讲解的这个异常,换一种布局文件的使用方式,即把上面的那一行代码注释掉,换成以下代码:
//获取Infalter对象
- LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
- LinearLayout parent = (LinearLayout) inflater.inflate(R.layout.main, null);
- ImageView child = (ImageView)parent.findViewById(R.id.child);
- setContentView(parent);
LayoutInflater为布局填充类,不明白的可以自己查,或者有机会我将在博客中介绍一下,然后是将main.xml文件inflate为LinearLayout文件,再得到child,即ImageView。然后就是通过调用setContentView(parent)将这个布局main.xml显示出来,这时得到的效果和仅使用setContentView(R.layout.main)这句代码得到的效果一样。
下面的操作将会出现异常了,大家注意:
- setContentView(child);
而这时在异常中它提示要再parent中调用removeView()。这里我们就听从指挥,在setContentView(child),之前添上一句parent.removeView(child),这时就不会再调用setContentView(child)就不会异常了,当然当前显示的将只是一幅图片了,而这时如果你调用setContentView(parent)的话将只显示文本内容,因为我们已经将child remove掉了嘛。
代码如下:
http://download.csdn.net/detail/yaolingrui/4129192
- 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 removeView
- 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 removeView(
- 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 removeView(
- 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 removeView(
- 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 removeView(
- 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 removeView(
- 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 removeView
- java.lang.IllegalStateException: The specified child already has a parent. You must call removeView(
- ava.lang.IllegalStateException: The specified child already has a parent. You must call removeView()
- ava.lang.IllegalStateException The specified child already has a parent. You must call removeView()
- Android—java.lang.IllegalStateException: The specified child already has a parent. You must call rem
- java console 没有消息到达的解决方案
- Spring Wrapper
- opengl es纹理贴图效果实例
- RMI(远程方法调用)实现简单的查单词功能
- 堆,栈,堆栈的区别
- java.lang.IllegalStateException The specified child already has a parent. You must call removeView()
- 基于大规模语料的新词发现算法
- 自定义JSTL函数
- java 类加载和初始化的那点秘密
- java动态代理(JDK和cglib)
- Python中使用中文
- STL中map的用法详解
- Google Maps 再度来袭(3D、离线、导航,什么都不是事)
- java静态/动态成员变量、初始化块,父类/子类构造函数执行顺序问题