布局引用引起错误

来源:互联网 发布:阿里云服务器关闭快照 编辑:程序博客网 时间:2024/05/29 21:32

错误日志

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.  

分析

由错误日志分析得,因当前引用布局已存在父布局,故引用前应在父布局执行removeView()方法,解除父布局与子布局关系,然后才可正确引用。

解决方法

ViewGroup parent = (ViewGroup) recyclerView.getParent();    if (parent != null) {        parent.removeView(recyclerView);}

注意

使用coordinatorLayout.removeView(recyclerView);看似正确,实则不行。

原创粉丝点击