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