1.main_layout_menubar.removeAllViews();

2.((LinearLayout) bookshelf_menubar.getParent()).removeAllViews(); 3.main_layout_menubar.addView(bookshelf_menubar);以上代码中,我利用动态加载把bookshelf_menubar加载到main_layout_menubar对象中,但它说bookshelf_menubar已有父对象,如下:
 The specified child already has a parent. You must call removeView() on the child's parent first.
所以我想出调用第二句,虽然没有报错了,但是bookshelf_menubar对象的功能全没了,我还要它执行业务逻辑呢,这该怎么解决呢?急,有哪位高手能不能帮忙解决一下的?先谢了!!!

解决方案 »

  1.   

    ViewGroup会检测加进来的对象有没Parent不未null时就会报上面的错误        if (child.getParent() != null) {
                throw new IllegalStateException("The specified child already has a parent. " +
                        "You must call removeView() on the child's parent first.");
            }
    你在removeAllViews()后检查下bookshelf_menubar.getParent()是不是未null
      

  2.   

    正常情况ViewGroup调用了removeAllViews()源码内会为每个view.mParent赋值为 null的
      

  3.   

    通过main_layout_menubar检查出其所有的child,然后逐个检测child的parent是否remove,没有就remove掉,然后接着你的思路就可了。