Fragment error:The specified child already has a parent.
来源:互联网 发布:jmeter for mac安装 编辑:程序博客网 时间:2024/05/29 18:02
前提:用RadioGroup写了一个底边导航,fragment_container用FrameLayout布局。
报错代码在:
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){mView = inflater.inflate(R.layout.fragment_registration, container);return mView;}这样,就会报错:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
但是,如果改成:
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){mView = inflater.inflate(R.layout.fragment_registration, null);return mView;}就可以成功编译。
为什么呢?
。。。。
下面解惑一下:
先看一Android Developer的描述:
public View inflate (int resource, ViewGroup root)
Inflate a new view hierarchy from the specified xml resource. Throws InflateException
if there is an error.
Parameters
R.layout.main_page
)Returns
- The root View of the inflated hierarchy. If root was supplied, this is the root View; otherwise it is the root of the inflated XML file.
public View inflate (int resource, ViewGroup root, boolean attachToRoot)
Inflate a new view hierarchy from the specified xml resource. Throws InflateException
if there is an error.
Parameters
R.layout.main_page
)Returns
- The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.
- The root View of the inflated hierarchy. If root was supplied, this is the root View; otherwise it is the root of the inflated XML file.
返回的是:填充的层次结构的根视图。如果root参数提供了,那么这就是根视图,否则将返回填充xml文件的根视图,即R.id.fragment_container.
通过测试得知:
onCreateView()中的ViewGroup container获取器id(即,container.getId())与R.id.fragment_container 是一致的。
另外,inflater.inflate(R.layout.fragment_one, container, true);
或者inflater.inflate(R.layout.fragment_one, container);这两种指定root的方法,在自定义控件时常被用到。
而在fragment中,不需要指定root。设为null即可。
即,inflater.inflate(R.layout.fragment_one, null, false);
或者inflater.inflate(R.layout.fragment_one, container, false);
或者inflater.inflate(R.layout.fragment_one, null);
- Fragment error:The specified child already has a parent.
- ViewPager Fragment IllegalStateException: 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
- 关于viewpager里嵌套fragment报 “The specified child already has a parent”的解决办法
- android Fragment java.lang.IllegalStateException:The specified child already has a parent.
- The specified child already has a parent错误
- IllegalStateException: The specified child already has a parent问题解决办法
- The specified child already has a parent错误
- AlertDialog异常The specified child already has a parent. ..解决方法
- The specified child already has a parent错误解决办法
- 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.解决方法
- ORA-27101: shared memory realm does not exist
- 看热闹dede模板下载与设置
- ibatis的探索和应用,实现ibatis的CRUD功能,ibatis的优缺点
- thread safe vs reentrant
- linux块设备驱动
- Fragment error:The specified child already has a parent.
- 微务宝开发“微活动”功能助推企业活动营销
- 关于MFC程序不能定位输入点
- 字符数组与字符串常量
- Direct3D编程第一个程序框架
- oracle to_date函数的用法
- IE浏览器不能显示所有图片
- google hacker技术
- 花开花落