关于异常“The specified child already has a parent. You must call removeView"的解决
2015-07-12 16:17
696 查看
在android开发过程中,有时会在不同情况下遇到同种问题:
[java]
view plaincopyprint?
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。
[java]
view plaincopyprint?
setContentView(R.layout.main);
这里为了解释今天要讲解的这个异常,换一种布局文件的使用方式,即把上面的那一行代码注释掉,换成以下代码:
//获取Infalter对象
[java]
view plaincopyprint?
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)这句代码得到的效果一样。
下面的操作将会出现异常了,大家注意:
[java]
view plaincopyprint?
setContentView(child);
而这时在异常中它提示要再parent中调用removeView()。这里我们就听从指挥,在setContentView(child),之前添上一句parent.removeView(child),这时就不会再调用setContentView(child)就不会异常了,当然当前显示的将只是一幅图片了,而这时如果你调用setContentView(parent)的话将只显示文本内容,因为我们已经将child remove掉了嘛。
[java]
view plaincopyprint?
java.lang.IllegalStateException The specified child already has a parent. You must call removeView() on the child's parent first.
java.lang.IllegalStateException The specified child already has a parent. You must call removeView() on the child's parent first.也就是非法状态异常,它说这个特定的child已经有一个parent了,你必须在这个parent中首先调用removeView()方法,才能继续你的内容。这里很明显这个child是一个View,一个子(child)View必须依赖于父(parent)View,如果你要使用这个child,则必须通过parent,而你如果就是硬想使用这个child,那么就得让这个child与parent脱离父子关系(即removeView())……算了还是举个简单的例子来说明一下,省的我说的乱七八糟,你听的也晕。
新建一个项目,布局文件中仅包含一个TextView和一个ImageView,布局方式是线性布局(具体可以参考后面的源代码),运行的结果就是显示一个文本和一张图片,
在Activity的onCreate()方法中,我们通常使用以下这种方式来使用布局文件main.xml。
[java]
view plaincopyprint?
setContentView(R.layout.main);
setContentView(R.layout.main);
这里为了解释今天要讲解的这个异常,换一种布局文件的使用方式,即把上面的那一行代码注释掉,换成以下代码:
//获取Infalter对象
[java]
view plaincopyprint?
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 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)这句代码得到的效果一样。
下面的操作将会出现异常了,大家注意:
[java]
view plaincopyprint?
setContentView(child);
setContentView(child);也就是将上文setContentView(parent),中的parent换成child。异常请看如下截图:
而这时在异常中它提示要再parent中调用removeView()。这里我们就听从指挥,在setContentView(child),之前添上一句parent.removeView(child),这时就不会再调用setContentView(child)就不会异常了,当然当前显示的将只是一幅图片了,而这时如果你调用setContentView(parent)的话将只显示文本内容,因为我们已经将child remove掉了嘛。
相关文章推荐
- Mysql 主主复制实现
- Microsoft Communications Control组件解决方法
- iOS略屌的面试题
- nodejs学习之模块:crypto和body-parser
- 习题3.18 检测平衡符号(/* */ 不知怎么做)
- shape 绘制图形
- java字符串缓冲池分析
- DownloadManager补漏
- Android fragment
- 希腊债务危机算个屁,中国股市现状才是全球经济最需要担忧的
- 你真的很熟分布式和事务吗?
- 关于SSH中schema约束配置错误导致
- xmpp和OpenFire示例,即时聊天室,支持离线消息
- Java基础整理篇1-比较字符串是否有重复
- HDU 2099 整除的尾数
- 1626 - Brackets sequence(DP)
- MySQL强制修改密码
- Swing-JRadioButton用法-入门
- poj 1273 Drainage Ditches(最大流入门)
- 一个newCachedThreadPool引发的案例