项目中遇到这个异常,查看了好久没找到原因,在添加view的地方也没找到错误,后来在一篇博客的评论中看到一个评论:出现此错误的根本问题是当前view已经在别的view 中,作为子view,而你的现在又要将当前view放在别的view中,就会抛出这类错误。解决方法是你先找到当前view的父view,在父view中调用removeview,移除当前view就可以解决此问题了。恍然大悟,马上找到问题。在fragment中设置布局时候写成了binding=DataBindingUtil.setConContentView(getActivity,R.layout.fragment_home);所以出现该错误,改成binding=DataBindingUtil.inflate(inflate,R.layout.fragment_home,container,attchToParent:false);问题就解决了,工作中一定要细心否则容易犯这种低级错误
The specified child already has a parent. You must call removeView() on the child's parent first.
最新推荐文章于 2024-05-24 17:09:53 发布