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>

今天写代码要给AlertDialog加一个自定义的View 然后就报错了,报错是因为再一次触发加载布局view的时候由于在viewgroup里已经有了这个布局所以不能再一次加载,必须先要移除,经过分析发现了解决办法

一开始是这样写的

<span style="font-size:24px;">view = getLayoutInflater().from(MainActivity2.this).inflate(R.layout.numberpicker,null);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity2.this);
</span>
<span style="font-size:24px;">builder.setView(view);</span>
后来这样写 就可以了
<span style="font-size:24px;">view = getLayoutInflater().from(MainActivity2.this).inflate(R.layout.numberpicker,null);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity2.this);
<span style="color:#ff6666;">ViewGroup vg=(ViewGroup)view.getParent();
if(vg!=null)
   {   vg.removeView(view);</span></span>
<span style="font-size:24px;color:#ff6666;">   }</span>
<span style="font-size:24px;">builder.setView(view);</span>

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

时间: 2024-11-05 15:58:28

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&#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

替换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  

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

Call removeView() on the child&#39;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.vie

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&quot;的解决

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

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