关于异常“The specified child already has a parent. You must call removeView"的解决(举例说明,附源码)
来源:互联网 发布:文件夹隐藏软件 编辑:程序博客网 时间:2024/04/27 23:04
在android开发过程中,有时会在不同情况下遇到同种问题:
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。
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);也就是将上文setContentView(parent),中的parent换成child。异常请看如下截图:
而这时在异常中它提示要再parent中调用removeView()。这里我们就听从指挥,在setContentView(child),之前添上一句parent.removeView(child),这时就不会再调用setContentView(child)就不会异常了,当然当前显示的将只是一幅图片了,而这时如果你调用setContentView(parent)的话将只显示文本内容,因为我们已经将child remove掉了嘛。
代码如下:
http://download.csdn.net/detail/yaolingrui/4129192
- 关于异常“The specified child already has a parent. You must call removeView"的解决(举例说明,附源码)
- 关于异常“The specified child already has a parent. You must call removeView"的解决(举例说明,附源码)
- 关于异常“The specified child already has a parent. You must call removeView"的解决(举例说明,附源码)
- 关于异常“The specified child already has a parent. You must call removeView"的解决
- 异常-The specified child already has a parent. You must call removeView
- The specified child already has a parent. You must call removeView() on the child's parent first.解决办
- The specified child already has a parent. You must call removeView() on the child's parent first.问解决
- 解决The specified child already has a parent. You must call removeView() on the child's parent first问题
- 嵌套Fragment的使用及遇到The specified child already has a parent. You must call removeView()问题的解决
- 嵌套Fragment的使用及遇到The specified child already has a parent. You must call removeView()问题的解决
- 嵌套Fragment的使用及遇到The specified child already has a parent. You must call removeView()问题的解决
- 嵌套Fragment的使用及遇到The specified child already has a parent. You must call removeView()问题的解决
- 解决viewpaper与frament时出现的The specified child already has a parent. You must call removeView()问题
- android viewp嵌套Fragment时遇到The specified child already has a parent. You must call removeView()问题的解决
- 【Android异常】The specified child already has a parent. You must call removeView() on the child's paren
- 【Android异常】The specified child already has a parent. You must call removeView() on the child's paren
- 【Android异常】The specified child already has a parent. You must call removeView() on the child's paren
- 【Android异常】The specified child already has a parent. You must call removeView() on the child's paren
- 从Nutch的输出日志分析其流程
- cocos2d 57 fonts
- Win下的输入法(IME)编程(2)
- Struts2 图片验证码
- cookie存储中文
- 关于异常“The specified child already has a parent. You must call removeView"的解决(举例说明,附源码)
- Android进阶2之PopupWindow弹窗(有点悬浮窗的感觉)
- 插入排序(java基础)
- 判断时间前后
- Shell排序(java基础)
- Linux进程控制
- Qt对字符的处理(beta1.0)
- 求m,n的最大公约数
- java路径问题