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
|
@Overridepublic 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."); }...... |
原来在中判断了child的parentview 必须为null,否则抛出一个异常。 也就是框架的设计者希望这里必须是一个没有parent的view,如果你不这么做,那么只好给你抛出个异常了,强制这样做也许是为了防止某些问题产生。addViewInner
bug_ _fragment_“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.
在ViewPager中,用Fragment显示页面时,报错: java.lang.IllegalStateException: The specified child already has a pa ...
- 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 ...
- 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 ...
- 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 ...
- 【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 ...
- 替换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); 错误 ...
- 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 ...
- 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, ...
- The specified child already has a parent错误
10-05 23:39:48.187: E/AndroidRuntime(12854): Caused by: java.lang.IllegalStateException: The specifi ...
随机推荐
- netsh winsock reset
最近要在虚拟机上安装 git ,然后托管一个项目,搞得我是焦头烂额.今天下午我卸载了vmware workstation ,但是奇怪的事就发生了,我的google chrome 浏览器没法浏览网页了. ...
- 对 Android 开发者有益的 40 条优化建议
本文转载于:http://www.oschina.net/translate/40-developer-tips-for-android-optimization?cmp 下面是开始Android编程 ...
- 异步IO/数据库/队列/缓存
同步与异步的性能区别 mport gevent def task(pid): """ Some non-deterministic task ""& ...
- Python编程规范(PEP8)
Python编程规范(PEP8) 代码布局 缩进 对于每一次缩进使用4个空格.使用括号.中括号.大括号进行垂直对齐,或者缩进对齐. 制表符还是空格? 永远不要将制表符与空格混合使用.Python最常用 ...
- 安装max plugin wizard
参考:http://liweizhaolili.blog.163.com/blog/static/162307442013117731953/
- 【转】[NOIP2003普及组]麦森数
来源:http://vivid.name/tech/mason.html 不得不纪念一下这道题,因为我今天一整天的时间都花到这道题上了.因为这道题,我学会了快速幂,学会了高精度乘高精度,学会了静态查错 ...
- 最短路径—Dijkstra算法和Floyd算法【转】
本文来自博客园的文章:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/31/2615833.html Dijkstra算法 1.定义概览 Dijk ...
- Flex 布局教程:语法篇(转)
网页布局(layout)是CSS的一个重点应用. 布局的传统解决方案,基于盒状模型,依赖 display属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如,垂直居中 ...
- 宝洁的Pvp
1.公司宗旨(Purpose) 我们生产和提供更佳品质及价值的产品,以改善全球消费者的生活.作为回报,我们将会获得领先的市场销售地位和利润增长,从而令我们的员工.我们的股东以及我们的生活.工作所处的社 ...
- Redis 实现高效不遗漏的事件封装
虽然Redis有订阅功能,但是订阅功能是实时的,过了这个点,就接收不到消息了. 同时,如果订阅的客户端因为某些特殊原因shutdown了,那也就找不回未处理完整的订阅事件了. 但好在,Redis还有一 ...