码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-10-29 19:00:32      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:blog   ar   使用   java   sp   div   on   log   ad   

产生这个异常的原因是使用的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"的解决

标签:blog   ar   使用   java   sp   div   on   log   ad   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!