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

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

时间:2015-05-31 12:28:30      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:android   安卓   java   

<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.

标签:android   安卓   java   

原文地址:http://blog.csdn.net/q15591/article/details/46287271

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