android Fragment java.lang.IllegalStateException:The specified child already has a parent.
来源:互联网 发布:linux关闭端口 编辑:程序博客网 时间:2024/06/16 10:21
在做项目的时候 ,做到了一个Activity里面有两个Tab切换,每一个Tab是一个Fragment展示内容,当两个Tab来回切换的时候,报了一个错误
经过查找原因,原来是Fragment中OnCreateView()的方法调用错了:
正确的方法应该是:
查阅多方资料得知,我们都LayoutInflater的使用存在误区
我们最常用的便是 LayoutInflater的inflate方法,这个方法重载了四种调用方式,分别为:
1. public View inflate(int resource, ViewGroup root)
2. public View inflate(int resource, ViewGroup root, boolean attachToRoot)
3.public View inflate(XmlPullParser parser, ViewGroup root)
4.public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)
这四种使用方式中,我们最常用的是第一种方式, inflate方法的主要作用就是将xml转换成一个View对象,用于动态的创建布局。虽然重载了四个方法,但是这四种方法最终调用的,还是第四种方式。第四种方式也很好理解,内部实现原理就是利用Pull解析器,对Xml文件进行解析,然后返回View对象。
inflate方法有三个参数,分别是
1. resource 布局的资源id
2. root 填充的根视图
3.attachToRoot是否将载入的视图绑定到根视图中
一般情况下,我们将BaseAdapter中的getView()方法:中根视图设置为null,
public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = inflate(R.layout.item_row, null); } return convertView;}和我们将根视图设置为false
@Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = inflater.inflate(R.layout.item_list, parent,false); } return convertView; }的区别是,如果在listview中的Item中根布局设置高度为40dp的话,第一种就没有效果。
经过看源码分析,当我们传进来的root参数不是空的时候,并且attachToRoot是false的时候,会重新给设置参数。
若我们采用 convertView = inflater.inflate(R.layout.item_list, null);方式填充视图,item布局中的根视图的layout_XX属性会被忽略掉,然后设置成默认的包裹内容方式。
除了使用这种方式,我们还可以设置item布局的根视图为包裹内容,然后设置内部控件的高度等属性,这样就不会修改显示方式了。
- android Fragment java.lang.IllegalStateException:The specified child already has a parent.
- java.lang.IllegalStateException: The specified child already has a parent.
- java.lang.IllegalStateException:The specified child already has a parent.
- java.lang.IllegalStateException: The specified child already has a parent
- Android异常分析之 java.lang.IllegalStateException:the specified child already has a parent.
- Android—java.lang.IllegalStateException: The specified child already has a parent. You must call rem
- Android | java.lang.IllegalStateException: The specified child already has a parent. 解决方案
- Android异常java.lang.IllegalStateException: The specified child already has a parent.
- mark一下android异常 java.lang.IllegalStateException: The specified child already has a parent. You must
- android异常:java.lang.IllegalStateException: The specified child already has a parent. You must call r
- Android开发——java.lang.IllegalStateException: The specified child already has a parent.
- ViewPager Fragment IllegalStateException: The specified child already has a parent
- 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
- java.lang.IllegalStateException The specified child already has a parent. You must call removeView()
- Caused by: java.lang.IllegalStateException: The specified child already has a parent. You
- 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(
- 从大数据到认知计算,未来需要更强的计算能力
- c++引用“&”
- java集合14--Map总结
- 源码编译安装lamp_第1篇(apache)
- Java---网络编程(2)-UDP
- android Fragment java.lang.IllegalStateException:The specified child already has a parent.
- 游戏角色 2
- 70. Climbing Stairs
- JDBC向数据库中插入数据
- 使用VS2015和Nhibernate实现与MySql数据库连接,实现增删改查操作
- 6个关于dd命令备份Linux系统的例子
- 62-Binary Tree Level Order Traversal
- 在执行期间制定文件位置并读取。利用BufferedReader与FileReader
- 求学的最后阶段,来一场说走就走的旅行,那该是一件多惬意的事