Call removeView() on the child's parent first

extends:http://stackoverflow.com/questions/6526874/call-removeview-on-the-childs-parent-first

Exception:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child‘s parent first.
           at android.view.ViewGroup.addViewInner(ViewGroup.java:1976)
           at android.view.ViewGroup.addView(ViewGroup.java:1871)
           at android.view.ViewGroup.addView(ViewGroup.java:1828)
           at android.view.ViewGroup.addView(ViewGroup.java:1808)
Solution:

((ViewGroup)scrollChildLayout.getParent()).removeView(scrollChildLayout); 

Use the child element to get a reference to the parent. Cast the parent to a ViewGroup so that you get access to the removeView method and use that.
 
     LinearLayout layout = initLayout();
        for (int i = 0; i < items.size(); i++) {
            View view = items.get(i);
            ((ViewGroup) view.getParent()).removeView(view);
            layout.addView(view, lpLabel);
        }
        mLayout.addView(layout);

Call removeView() on the child's parent first

时间: 2024-10-12 19:54:58

Call removeView() on the child's parent first的相关文章

Android-The specified child already has a parent. You must call removeView() on the child&#39;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

The specified child already has a parent. You must call removeView() on the child&#39;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  

Child &lt;- many-to-one -&gt;Parent

网上找到个描述的很精妙的例子 Child   <-   many-to-one   ->Parent         class   Child   {         private   Parent   parent;             public   Parent   getParent   (){             return   this.parent;//访问了实例变量         }             }         class   Parent  

Child extends Parent,可以得到什么?

如果有Child extends Parent 1.子类可以调用父类无参的构造函数,子类的有参构造函数和是否调用父类的有参数的构造函数无必然联系 2.接口继承的时候,只能继承接口不能继承类,因为如果类可以存在非抽象的成员,如果接口继承了该类,那么接口也必定从类中也继承了这些非抽象成员,这就和接口的定义相矛盾,所以接口继承时,只能继承接口不能继承类. 3.接口只能继承接口,但是可以多继承.类都是单继承,但是继承具有传递性 4.一个类一旦没有显式的定义任何构造,那么JVM会默认给你一个无参构造.无参

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()) 何时会出现这种异常呢,典型的

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代码中假设出现此异常,说明在同一个布局中加入了同样的组件实例.应该创建不同的实例组件,并将其加入到布局其中.

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()方法,