title: IllegalStateException when trying to addView() with compound view
body: <p>I am trying to get a <code>LinearLayout</code> to be added to the main <code>Activity</code> view when a button is pressed. </p>
  
  <p><strong>NOTE</strong> before trying this with <code>merge</code> I used <code>LinearLayout</code> with the same results. I read this <a href="https://stackoverflow.com/questions/17355423/using-custom-compound-view-as-a-listviews-item">post</a> which made me thing <code>merge</code> would help.</p>
  
  <p>The XML for the <code>View</code> I want to add looks like this.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;merge xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/grocery_list_row"&gt;
  
      &lt;EditText
          android:hint="Price"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" /&gt;
  
      &lt;EditText
          android:hint="Item"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" /&gt;
  
      &lt;CheckBox
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" /&gt;
  &lt;/merge&gt;
  </code></pre>
  
  <p>The java for this <code>View</code> looks like so</p>
  
  <pre><code>package com.example.developer.grocerylist.impl;
  
  import android.content.Context;
  import android.util.AttributeSet;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.widget.LinearLayout;
  
  import com.example.developer.grocerylist.R;
  
  /**
   * Created by developer on 8/16/2015.
   */
  public class GroceryListTableRowImpl extends LinearLayout {
  
      int mRows;
  
      public GroceryListTableRowImpl(Context context) {
          this(context, null);
      }
  
      public GroceryListTableRowImpl(Context context, AttributeSet attributeSet) {
          super(context, attributeSet);
      }
  
      public GroceryListTableRowImpl(Context context, AttributeSet attributeSet, int defStyles) {
          super(context, attributeSet, defStyles);
      }
  
      public void addRow(){
          mRows++;
          removeAllViews();
          for(int i = 0; i &lt; mRows; i++){
              addView(createRow());
          }
      }
  
      private View createRow() {
          View v;
          LayoutInflater inflater = LayoutInflater.from(getContext());
          v = inflater.inflate(R.layout.grocery_list_row,this,true);
          return v;
      }
  }
  </code></pre>
  
  <p>The main <code>Activity</code>'s XML looks like this</p>
  
  <pre><code>&lt;TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:paddingBottom="@dimen/activity_vertical_margin"
      tools:context=".GroceryList"
      android:id="@+id/list_table"&gt;
  
      &lt;com.example.developer.grocerylist.impl.GroceryListTableRowImpl
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/grocery_list"&gt;
  
      &lt;/com.example.developer.grocerylist.impl.GroceryListTableRowImpl&gt;
  
  
      &lt;TableRow
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"&gt;
          &lt;DigitalClock
              android:textSize="20pt"
              android:layout_height="wrap_content"
              android:layout_width="fill_parent"/&gt;
      &lt;/TableRow&gt;
  
      &lt;TableRow
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"&gt;
          &lt;TextView
              android:id="@+id/textview"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:minWidth="250px"
              android:textSize="20pt"
              android:paddingBottom="5px"
              android:text=""
              android:background="#777777"
              android:textColor="#ff0000" /&gt;
  
          &lt;Button
              android:id="@+id/add"
              android:text="add" /&gt;
      &lt;/TableRow&gt;
  &lt;/TableLayout&gt;
  </code></pre>
  
  <p>Finally the main <code>Activity</code>'s java</p>
  
  <pre><code>package com.example.developer.grocerylist.activities;
  
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.view.ViewGroup;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TableLayout;
  import android.widget.TextView;
  
  import com.example.developer.grocerylist.R;
  import com.example.developer.grocerylist.api.GroceryListTableRow;
  import com.example.developer.grocerylist.impl.GroceryListTableRowImpl;
  
  
  public class GroceryList extends AppCompatActivity implements OnClickListener {
  
      TextView textView = null;
      GroceryListTableRowImpl groceryListTableRow = null;
      Button add = null;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_grocery_list);
  
          textView = (TextView)findViewById(R.id.textview);
          groceryListTableRow = (GroceryListTableRowImpl)findViewById(R.id.grocery_list);
          add = (Button)findViewById(R.id.add);
          add.setOnClickListener(this);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.menu_grocery_list, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          //noinspection SimplifiableIfStatement
          if (id == R.id.action_settings) {
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
  
      @Override
      public void onClick(View v) {
          groceryListTableRow.addRow();
      }
  }
  </code></pre>
  
  <p>When I boot up the App and click the button I get this</p>
  
  <pre><code>Process: com.example.developer.grocerylist, PID: 2353
  java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
          at android.view.ViewGroup.addViewInner(ViewGroup.java:3936)
          at android.view.ViewGroup.addView(ViewGroup.java:3786)
          at android.view.ViewGroup.addView(ViewGroup.java:3727)
          at android.view.ViewGroup.addView(ViewGroup.java:3700)
          at com.example.developer.grocerylist.impl.GroceryListTableRowImpl.addRow(GroceryListTableRowImpl.java:34)
          at com.example.developer.grocerylist.activities.GroceryList.onClick(GroceryList.java:61)
          at android.view.View.performClick(View.java:4780)
          at android.view.View$PerformClick.run(View.java:19866)
          at android.os.Handler.handleCallback(Handler.java:739)
          at android.os.Handler.dispatchMessage(Handler.java:95)
          at android.os.Looper.loop(Looper.java:135)
          at android.app.ActivityThread.main(ActivityThread.java:5257)
          at java.lang.reflect.Method.invoke(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:372)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
  </code></pre>
  
  <p>I know this is a lot to take in. I have been scratching my head on this one. I have never made a compound view before. That might be evident. Any help would be greatly appreciated. Thanks in advance!</p>
  
  <p><strong>UPDATE</strong> When debugging when I get to the <code>addView(createRow())</code> line things fail. It does not seem to be making it into the <code>createRow()</code> method.</p>