报错信息为:java.lang.IllegalStateException: The specified child already has a parent.
来源:互联网 发布:淘宝网尼大衣 编辑:程序博客网 时间:2024/05/10 15:22
报错信息为:java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child’s parent first.
首先,如果你看到这篇文章,说明你已经搜了很多篇了(首页可能字数都不够,但是又不甘心收获不能普度众生……),所以我想声明一下,你一定不要抱着直接找到源码解决问题的想法,要找方法,发现问题解决问题的方法!
好了,正如报错信息提示的,说明你现在要操作的这个View已经有了一个父View,所以你要找是不是那个地方,比如创建这个View时,调用了addView的方法等等,使它有了一个不知名的父类(只是你不知道而已)
这种问题普遍出现在动态添加View时,会出现。比如,有些项目把某一个控件如LinearLayout等,弄出来当作全局变量了,以便在其他地方直接调用addView进行动态添加,注意如果你在声明LinearLayout 时,试着紧跟着它动态添加一个view,没问题——那说明全局调用也绝对能用!所以,问题一定出在你创建自己定义的View时调用了addView等等别的方法,使得其有了父类,抑或是其本来就有父类,你不注意,所以自然要报错。
比如一下就是从网上直接拷来的自定义View的模板
public class CustomView extends RelativeLayout{ View mView=null; public Weather(Context context) { super(context); LayoutInflater mInflater = LayoutInflater.from(context); mView = mInflater.inflate(R.layout.weather, null); addView(mView); }
你要是使用这个自定义控件肯定不能通过addView添加到其他的布局中去,会报如题的错误,因为它已经有父类了(代码中addView(myView);使它有了父类,虽然没人知道其父类是谁~),所以必须要注释掉addView(myView);这个CustomView才是一个独立的View。
- 报错信息为: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 RecyclerView 报错:java.lang.IllegalStateException: The specified child already has a...
- 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
- Android异常分析之 java.lang.IllegalStateException:the specified child already has a parent.
- 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(
- 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(
- Android | java.lang.IllegalStateException: The specified child already has a parent. 解决方案
- java.lang.IllegalStateException: The specified child already has a parent. You must call removeView(
- 高效的平衡搜索树——红黑树
- LeetCode进阶之路(Merge Two Sorted Lists)
- 将xml布局转换成view对象
- android中的jni开发(简版)
- 移植UBoot的2016版本/添加新的板级支持
- 报错信息为:java.lang.IllegalStateException: The specified child already has a parent.
- 复制表数据、表结构;临时表创建
- shell script input and out put functuo
- lightoj1079【背包】
- MySQL数据安装卸载解决方案以及基本增删改查
- Android获取系统当前时间
- Android-5.1-bootchart
- TCP协议
- Android网络之数据解析----使用Google Gson解析Json数据