mark一下android异常 java.lang.IllegalStateException: The specified child already has a parent. You must
来源:互联网 发布:命令行设置ip地址和mac 编辑:程序博客网 时间:2024/06/03 06:27
一直都是把笔记记在自己的本本里,今天开始,决定把它写出来,能够自己提高,也可以分享经验,第一次写博客,写得不好请见谅。
今天分享的是关于最近遇到的一个代码异常
在RecyclerView中通过自定义的HeadAdapter添加一个recycleview作为HeadView时抛出的异常
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
意思大概是,这个特定的子view已经有一个父view了,你必须先在父view中调用removeView()函数才能在其他把它设为其他view的子view
接下来是抛出异常的代码(只写出了关键代码)
View Header=LayoutInflater.from(mContext).inflate(R.layout.headview,mRecyclerView,false); **RecyclerView recyclerView= (RecyclerView) Header.findViewById(R.id.head_recycle);** recyclerView.setHasFixedSize(true); recyclerView.setLayoutManager(new LinearLayoutManager(ThemesDetailActivity.this,LinearLayoutManager.HORIZONTAL,false)); ThemesDetailsHeadAdapter themesDetailsHeadAdapter=new ThemesDetailsHeadAdapter(recyclerView,mEditorses); recyclerView.setAdapter(themesDetailsHeadAdapter); **mHeaderViewRecyclerAdapter.addHeaderView(recycleView);**
其中加粗的即为引起出错的代码
代码中可以看到,Header是通过inflate函数创建的,紧接着,通过Header的findviewbyId方式获得了recycleview的实例,注意,这里即指定其父view为Header,接着调用mHeaderViewRecyclerAdapter.addHeaderView的时候,mRecyclerView直接添加了recycleview作为其子view,导致异常。
这里的关系是,Header作为mRecyclerView的子view,而recycleview又是Header的子view,如果把recycleview直接作为mRecyclerView的子view添加进去,当然就报错了
最后,理解下这几个函数的区别,可以加深对定义view的理解
1.LayoutInflater.from(mContext).inflate(xmlId,null)2.LayoutInflater.from(mContext).inflate(xmlId,parent)3.LayoutInflater.from(mContext).inflate(xmlId,parent,null)4.LayoutInflater.from(mContext).inflate(xmlId,parent,false)5.LayoutInflater.from(mContext).inflate(xmlId,null,false)6.LayoutInflater.from(mContext).inflate(xmlId,null,true)
1.只通过xmlId创建一个view,并返回该view
2.通过xmlId创建一个view,然后把它添加为parent的子view,并返回parent
3.通过xmlId创建一个view,然后把它添加为parent的子view,然后执行view.setLayoutParams(params)(即可以设置view在父view中的属性,如layout_width,layout_herght),并返回view
4.通过xmlId创建一个view,然后把它添加为parent的子view,并返回parent
5.只通过xmlId创建一个view,并返回该view
6.只通过xmlId创建一个view,并返回该view
虽然只是个小错误,但是值得温习的还是很多的,mark一下~
- 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. You must call rem
- 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()
- 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(
- 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(
- 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(
- java.lang.IllegalStateException: The specified child already has a parent. You must call removeView(
- 关于RecyclerView的java.lang.IllegalStateException: The specified child already has a parent. You must c
- java.lang.IllegalStateException: The specified child already has a parent. You must call removeView
- 自定义异常类
- 匈牙利原理
- mac Mysql密码修改
- input--筛选
- 每天一个小程序——第 0000 题:添加角标数字
- mark一下android异常 java.lang.IllegalStateException: The specified child already has a parent. You must
- MyBatis学习总结(八)——Mybatis3.x与Spring4.x整合
- javascript中组合使用构造函数模式和原型模式创建对象
- LLDP协议认识
- poj2406--kmp next的应用
- VC++ replace和replaceAll两个方法的区别
- c#开发-实现从FTP服务器上下载文件夹中的数据
- spring3之后各jar包相着依赖关系
- ab压力测试