这个异常的出现往往是因为非法使用了某些方法引起的。

从字面意思上是说这个特定的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,如果你不这么做,那么只好给你抛出个异常了,强制这样做也许是为了防止某些问题产生。

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

  1. java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

    在ViewPager中,用Fragment显示页面时,报错: java.lang.IllegalStateException: The specified child already has a pa ...

  2. android 异常信息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's p ...

  3. fragment The specified child already has a parent. You must call removeView()

    在切换Fragment的时候出现:The specified child already has a parent. You must call removeView()异常. 错误主要出在Fragm ...

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

  5. 【Android异常】The specified child already has a parent. You must call removeView() on the child's parent first.

    错误信息: Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must ...

  6. 替换Fragment 报错 The specified child already has a parent. You must call removeView() on the child's parent first.

    在将一个fragment替换到一个frameLayout的时候报错: code: transaction.replace(R.id.fragment_container, fragment2); 错误 ...

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

    这个问题搞了我半天了,网上有很多人说需要找到该控件的parent后,让该parent 先remove需要添加的控件,然后在添加,如: if (view != null) { ViewGroup par ...

  8. Fragment+FragmentActivity出现The specified child already has a parent. You must call removeView() on the child's parent first.

    这个异常是出现在Fragment中的onCreateView方法中初始化布局时发生的. View view = inflater.inflate(R.layout.fragment3_layout, ...

  9. The specified child already has a parent错误

    10-05 23:39:48.187: E/AndroidRuntime(12854): Caused by: java.lang.IllegalStateException: The specifi ...

随机推荐

  1. 深入理解html5系列-文本标签

    转:http://blog.csdn.net/lihui130135/article/details/45150501 文章简介:       关于html5相信大家早已经耳熟能详,但是他真正的意义在 ...

  2. java的nio之:浅析I/O模型

    也许很多朋友在学习NIO的时候都会感觉有点吃力,对里面的很多概念都感觉不是那么明朗.在 进入Java NIO编程之前,我们今天先来讨论一些比较基础的知识:I/O模型.下面本文先从同步和异步的概念 说起 ...

  3. Scala vs. Groovy vs. Clojure

    http://stackoverflow.com/questions/1314732/scala-vs-groovy-vs-clojure Groovy is a dynamically typed ...

  4. HTTP头详解

    HTTP 头部解释 1. Accept:告诉WEB服务器自己接受什么介质类型,*/* 表示任何类型,type/* 表示该类型下的所有子类型,type/sub-type. 2. Accept-Chars ...

  5. Java 编程实践

    创建一个54个元素的整数数组,并将其元素值依次赋值为:1~54,用于表示一副牌的54张.再创建一个12个元素的整数数组,用于表示某玩家手中的牌,然后从前一数组中随机抽取12个元素赋值给该数组.打印后一 ...

  6. Presto 来自Facebook的开源分布式查询引擎

    Presto是一个分布式SQL查询引擎, 它被设计为用来专门进行高速.实时的数据分析.它支持标准的ANSI SQL,包括复杂查询.聚合(aggregation).连接(join)和窗口函数(windo ...

  7. 触发器 'SA.U_USER_INFO_TRG' 无效且未通过重新验证--Oracle序列

    程序开发时报错:触发器 'SA.U_USER_INFO_TRG' 无效且未通过重新验证打开触发器的定义,执行其中的语句,发现序列 U_USER_INFO_SEQ 未定义.什么是序列呢?序列相当于sql ...

  8. 【extjs】 extjs5 Ext.grid.Panel 搜索示例

    先看效果图: 页面js: <script type="text/javascript"> /** * 日志类型 store * */ var logTypeStore ...

  9. Debian环境下vi设置

    下面给出一个vi编辑器的配置文件,可以放到用户目录的.vimrc文件中: "========================================================= ...

  10. Servlet Filter 3

    11.MD5加密 /** * 使用md5的算法进行加密 */ public static String md5(String plainText) { byte[] secretBytes = nul ...