您的位置:首页 > 移动开发 > Android开发

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

2016-07-27 11:13 471 查看
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,一个子(child)View必须依赖于父(parent)View,如果你要使用这个child,则必须通过parent,另外要是硬想使用这个child,就得让这个child与parent脱离父子关系(即removeView())

我的代码如下 

button.setWidth(50);

        button.setHeight(250);

        button.setBackgroundColor(Color.BLUE);

        linearLayout.addView(button);

        setContentView(button);    //linearLayout,button也是view的子类

在最后一句报错,因为button是linearLayout的子类,要使用这个button就要通过父类实现
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: