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

后端 未结 13 2087
余生分开走
余生分开走 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:27

    You dont need this line: setContentView(txtCambiado);

    0 讨论(0)
  • 2020-11-29 08:33

    In my case i just pass the wrong view to the Method

    0 讨论(0)
  • 2020-11-29 08:35

    I encountered this error whenever I omitted a parameter while inflating the view for a fragment in the onCreateView() method like so:

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_reject, container);
        return view;
    }
    

    The solution is to change the view inflation line to:

    View view=inflater.inflate(R.layout.fragment_reject, container,false);

    The explanation can be found at the Android guide for fragments

    Quoting from the guide, the final parameter in the view initialization statement is false because:

    "the system is already inserting the inflated layout into the container—passing true would create a redundant view group in the final layout"

    0 讨论(0)
  • 2020-11-29 08:35

    Try this way,hope this will help you to solve your problem.

    TextView textView = new TextView(this);
    textView.setText("CustomTextView");
    addContentView(textView,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    
    0 讨论(0)
  • 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()
    }
    
    0 讨论(0)
  • 2020-11-29 08:38

    in ActivitySaludo, this line,

        setContentView(txtCambiado);
    

    you must set the content view for the activity only once.

    0 讨论(0)
提交回复
热议问题