新手Android学习笔记 05——IllegalStateException非法异常之一

来源:互联网 发布:玻璃笔淘宝 编辑:程序博客网 时间:2024/06/15 05:29
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

非法状态异常,这个特定的child已经有一个parent了,你必须在这个parent中首先调用removeView()方法。这里很明显这个child是一个View,一个子(childView必须依赖于父(parentView,如果你要使用这个child,则必须通过parent,另外要是硬想使用这个child,就得让这个childparent脱离父子关系(即removeView())

我的代码如下 

button.setWidth(50);
        button.setHeight(250);
        button.setBackgroundColor(Color.BLUE);
        linearLayout.addView(button);
        setContentView(button);    //linearLayout,button也是view的子类

在最后一句报错,因为button是linearLayout的子类,要使用这个button就要通过父类实现

0 0
原创粉丝点击