【错误】The specified child already has a parent.
来源:互联网 发布:微信分销源码下载 编辑:程序博客网 时间:2024/06/10 02:43
【错误】The specified child already has a parent.
错误提示:java.lang.IllegalStateException:The specified child already has a parent.
出错的可能地点:Inflater.inflater() 主要是在fragment 中。或者ListView之中。
解决办法:
将 view=inflater.inflate(R.layout.fragment_in_theaters,container);
改为 view=inflater.inflate(R.layout.fragment_in_theaters,container,false);
注:R.layout.fragment_in_theaters 为你所填充的布局。container 父布局
错误原因:这个错误出现的原因,主要是由于对Inflater类的认识不够深入。而LayoutInflater类是在我们需要使用Fragment和listView等控件时常常需要使用的一个工具类。而这个类主要的作用就是将XML中的布局填充到父布局中去。
要分析错误出现的原因我们可以从源码中入手。
//一开始出错调用的方法 public View inflate(XmlPullParser parser, @Nullable ViewGroup root) { return inflate(parser, root, root != null); }//正确调用时使用的方法public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) { final Resources res = getContext().getResources(); if (DEBUG) { Log.d(TAG, "INFLATING from resource: \"" + res.getResourceName(resource) + "\" (" + Integer.toHexString(resource) + ")"); } final XmlResourceParser parser = res.getLayout(resource); try { return inflate(parser, root, attachToRoot); } finally { parser.close(); }
可以看到实际上两者最后都是调用了同一个方法
return inflate(parser, root, root != null);
return inflate(parser, root, attachToRoot);
而当我们在Fragment中填充布局时,父布局不为空时。那么我们传入的参数对于这两个方法的区别在于第三个参数 前者为true 后者为false(参见一开始传入的参数)。
那么接下来我们可以进入这个真正被调用的方法中看一看。
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) { //省略一大部分代码 if (root != null && attachToRoot) { root.addView(temp, params); } //省略之后的代码
在实际调用当中我们可以看到,当root不为空,attachToRoot为true时将调用root.addView()方法。
而实际上的问题就发生在这之中。
- The specified child already has a parent错误
- The specified child already has a parent错误
- The specified child already has a parent错误解决办法
- The specified child already has a parent..."错误
- 【错误】The specified child already has a parent.
- The specified child already has a parent.
- The specified child already has a parent
- The specified child already has a parent
- The specified child already has a parent
- The specified child already has a parent. You must call removeView() on the child's parent first.错误
- Fragment error:The specified child already has a parent.
- IllegalStateException: The specified child already has a parent问题解决办法
- AlertDialog异常The specified child already has a parent. ..解决方法
- ViewPager Fragment IllegalStateException: The specified child already has a parent
- IllegalStateException :the specified child already has a parent
- IllegalStateException: The specified child already has a parent问题解决办法
- java.lang.IllegalStateException: The specified child already has a parent.
- Android The specified child already has a parent.解决方法
- SpringMVC WebSocket Apache代理 相关技术点总结
- 博客迁移通知
- Add Strings
- 5-1 抢红包 (25分)
- 大数据告诉你中国人喜欢用什么来刮胡子
- 【错误】The specified child already has a parent.
- 7.11--SSH学习之Struts拦截器
- R中字段抽取、字段合并、字段匹配
- CSS样式设置小技巧
- canvas简单模拟太阳地球月亮的转动
- dump生成与分析
- [C++]MYSQL 数据库操作封装及连接池实现
- ta_lib安装与测试
- Windows编程之旅(七)