首页 > 其他 > 详细

“The specified child already has a parent. You must call removeView"的解决

时间:2014-10-29 18:57:19      阅读:339      评论:0      收藏:0      [点我收藏+]

产生这个异常的原因是使用的view已经包含在其他的view中里面了,在将这个view添加到目标view中前,需要先将这个view与他的parent view的关系解除。

        child = (MapView) findViewById(R.id.child);
        setContentView(child);

  在这个时候会抛出The specified child already has a parent. You must call removeView这样的异常,原因在于child这个view包含在另外一个view中,解决方法如下:

        child = (MapView) findViewById(R.id.child);
        LinearLayout parent = (LinearLayout) child.getParent();
        parent.removeView(child);
        setContentView(child);    

  先使用child.getParent()方法获取child关联的view,然后调用removeView移除两个view之间的关系,然后就可以进行添加了。

“The specified child already has a parent. You must call removeView"的解决

原文:http://www.cnblogs.com/pkpokemon/p/4059835.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!