title: Conflicting Android error messages: The specified child already has a parent. You must call removeView() on the child's parent first
body: <p>Originally I got this error:</p>
  
  <blockquote>
    <p>The specified child already has a parent. You must call removeView()
    on the child's parent first</p>
  </blockquote>
  
  <p>at </p>
  
  <pre><code>customSection.addView(customLayout);
  </code></pre>
  
  <p>So I added</p>
  
  <pre><code>((LinearLayout)customLayout.getParent()).removeView(customLayout);
  </code></pre>
  
  <p>and now get</p>
  
  <pre><code>java.lang.NullPointerException
  </code></pre>
  
  <p><strong>So if the child has a parent, and I must first remove the child from the parent, why does getParent() return null?</strong></p>
  
  <p>I have an abstract fragment that allows derived classes to supply a custom layout for the list adapter. Relevant code:</p>
  
  <p>Binding:</p>
  
  <pre><code>public void bind(DataObject row) {
      View customLayout = getChildItemView(row);
      if (customLayout != null) {
          ((LinearLayout) customLayout.getParent()).removeView(customLayout);
          customSection.removeAllViews();
          customSection.addView(customLayout);
          customSection.setVisibility(View.VISIBLE);
      } else {
          customLayout.setVisibility(View.INVISIBLE);
      }
  
  }
  
  protected View getChildItemView(CommonRow row) {
      if (parentView == null) {
          parentView = (LinearLayout) LayoutInflater.from(getActivity())
                  .inflate(R.layout.list_item_custom_section,
                          new LinearLayout(getActivity()), true);
          label = (TextView) parentView.findViewById(R.id.txtData1Label);
          value = (TextView) parentView.findViewById(R.id.txtData1Value);
      }
      label.setText("Minimum");
      value.setText(manager.formatMoney(((SpecificDataRow) row).minimum));
      return parentView;
  }
  </code></pre>
  
  <p>I've also tried <code>inflater.inflate(R.layout.list_item_custom_section, null)</code> ... false, null / false, what gives?</p>
  
  <p>EDIT:</p>
  
  <p>@allprog, I knew some cleanup was needed. I wrote this at the end of the day somewhat in a hurry. I have since cleaned up the code, and separated out the binding and inflating of the view. Cleaned up code:</p>
  
  <pre><code>private class ViewHolder {
  ....
  
         public ViewHolder(View v) {
              Butterknife.inject(this, v);
              View custom = createCustomView(customSection);
              if (custom != null) {
                  customSection.setVisibility(View.VISIBLE);
                  customSection.addView(custom);
              }
          }
  
          public void bind(CommonRow row) {
              ......
  
              bindCustomView(row, customSection);
          }
  
  }
  </code></pre>
  
  <p>Child class:</p>
  
  <pre><code>    @Override
      protected View createCustomView(ViewGroup parent) {
          return LayoutInflater.from(getActivity()).inflate(R.layout.list_item_custom_section, parent, false);
      }
  
  
      @Override
      protected void bindCustomView(CommonRow row, ViewGroup section) {
          TextView label = Views.findById(section, R.id.txtData1Label);
          TextView value = Views.findById(section, R.id.txtData1Value);
  
          label.setText("Minimum");
          value.setText(manager.formatMoney(((SpecificRow) row).minimum));
      }
  </code></pre>
  
  <p>suitianshi got it first, with my original [unkempt] code that was the solution.</p>