<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