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())

何时会出现这种异常呢,典型的是在使用Fragment的时候,在Fragment的onCreateView中有这样一段代码:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

        Bundle savedInstanceState) {

    LinearLayout drawerListViewContainer = (LinearLayout) inflater.inflate(

            R.layout.fragment_navigation_drawer, container, false);

    mDrawerListView = (ListView)drawerListViewContainer.findViewById(R.id.drawer_list);

    mDrawerListView

            .setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override

                public void onItemClick(AdapterView<?> parent, View view,

                        int position, long id) {

                    selectItem(position);

                }

            });

                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

    SimpleAdapter mDrawerAdapter =  new SimpleAdapter(getActivity(),getDrawerItems(),R.layout.drawer_item,new String[]{"img","title"},new int[]{R.id.item_icon,R.id.item_name});

    mDrawerListView.setAdapter(mDrawerAdapter);

    mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);

    return mDrawerListView;

}

这段代码运行之后即会报出这样的错误

这是因为,返回的mDrawerListView他有一个parent,正确的做法是返回drawerListViewContainer,其实这也是我的本意,因为大意才写成了返回mDrawerListView

不过仔细想想,为什么就不能返回mDrawerListView呢,都是view 没有本质区别啊,这还得看出现这个异常信息的地方发生了什么事,从上面的log中我们知道这个异常产生在ViewGroup的addViewInner方法中。addViewInner的代码如下(部分):


1

2

3

4

5

6

7

8

9

10

11

12

    private void addViewInner(View child, int index, LayoutParams params,

            boolean preventRequestLayout) {

        if (mTransition != null) {

            // Don‘t prevent other add transitions from completing, but cancel remove

            // transitions to let them complete the process before we add to the container

            mTransition.cancel(LayoutTransition.DISAPPEARING);

        }

        if (child.getParent() != null) {

            throw new IllegalStateException("The specified child already has a parent. " +

                    "You must call removeView() on the child‘s parent first.");

        }

......

原来在addViewInner中判断了child的parentview 必须为null,否则抛出一个异常。 也就是框架的设计者希望这里必须是一个没有parent的view,如果你不这么做,那么只好给你抛出个异常了,强制这样做也许是为了防止某些问题产生。

时间: 2024-12-19 19:07:16

bug_ _fragment_“The specified child already has a parent. You must call removeView"的解决以及产生的原因的相关文章

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

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包含在

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  

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