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

产生这个异常的原因是使用的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之间的关系,然后就可以进行添加了。

时间: 2024-11-07 17:49:10

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

bug_ _fragment_“The specified child already has a parent. You must call removeView"的解决以及产生的原因

这个异常的出现往往是因为非法使用了某些方法引起的. 从字面意思上是说这个特定的child已经有一个parent了,你必须在这个parent中首先调用removeView()方法,才能继续你的内容.这里很明显这个child是一个View,一个子(child)View必须依赖于父(parent)View,如果你要使用这个child,则必须通过parent,而你如果就是硬想使用这个child,那么就得让这个child与parent脱离父子关系(即removeView()) 何时会出现这种异常呢,典型的

Android-The specified child already has a parent. You must call removeView() on the child's parent first.

这个问题搞了我半天了,网上有很多人说需要找到该控件的parent后,让该parent 先remove需要添加的控件,然后在添加,如: if (view != null) { ViewGroup parent = (ViewGroup) view.getParent(); if (parent != null) { parent.removeView(view); } } try { view = inflater.inflate(R.layout.fragment_main, container

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView

?? java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 在android代码中假设出现此异常,说明在同一个布局中加入了同样的组件实例.应该创建不同的实例组件,并将其加入到布局其中.

The specified child already has a parent. You must call removeView() on the child's parent first.

<pre name="code" class="java"><span style="font-size:24px;">java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.</span> 今天写代码要给Al

替换Fragment 报错 The specified child already has a parent. You must call removeView() on the child&#39;s parent first.

在将一个fragment替换到一个frameLayout的时候报错: code: transaction.replace(R.id.fragment_container, fragment2); 错误:  java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 原因: @Override  

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中的oncreateview方法中. 可能引起错误的写法: View view = inflater.inflate(R.layout.indexfragment_layout, container); 正确写法: View view = inflater.inflate(R.layout.i

android viewp嵌套Fragment时遇到The specified child already has a parent. You must call removeView()问题的解决

<span style="font-size:18px;">@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (root == null) { root = inflater.inflate(R.layout.fragment_pull_listview, container, false); } e

Android IllegalStateException: The specified child already has a parent问题解决办法

最近遇到一个很让人头疼的问题,使用viewpager动态添加页面或者删除页面时出现了问题(java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first),在stackoverflow上找到了解决办法.(http://stackoverflow.com/questions/22936886/java-l

关于异常“The specified child already has a parent. You

在android开发过程中,有时会在不同情况下遇到同种问题: [java] view plaincopy java.lang.IllegalStateException The specified child already has a parent. You must call removeView() on the child's parent first. 也就是非法状态异常,它说这个特定的child已经有一个parent了,你必须在这个parent中首先调用removeView()方法,