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

后端 未结 13 2149
余生分开走
余生分开走 2020-11-29 07:50

I create this post, because i am new at this, and i need a little help. I am doing a little exercise about a application you that put your name, and it returns \"hello (the

13条回答
  •  隐瞒了意图╮
    2020-11-29 08:37

    I’ve found a solution. I had to clear fragment’s parent from views before destroying and I’ve used the next peace of code for this:

    in Java

    public void onDestroyView() {
            if (rootView != null){
                ViewGroup viewGroup = (ViewGroup)rootView.getParent();
                if (viewGroup != null){
                    viewGroup.removeAllViews();
                }
            }
            super.onDestroyView();
        }
    

    in Kotlin

    override fun onDestroyView() {
        if (rootView != null) {
            val viewGroup = rootView.parent as ViewGroup?
            viewGroup?.removeAllViews();
        }
        super.onDestroyView()
    }
    

提交回复
热议问题