title: Adding Fragments to a layout
body: <p>I am trying to make my app more tablet-friendly, and so I'm trying to learn fragments. I want the typical two-pane layout, where the left side is the "navigation", and you click on one of the elements, and it changes the fragment on the right.</p>
  
  <p>I can duplicate the tutorials that use a ListFragment for the left, and if you click on one of them, it updates the "details" fragment on the right.</p>
  
  <p>I've tried the best I can to duplicate that code, and just use a LinearLayout, with buttons for the left side, so that if a button is clicked, it loads the appropriate fragment on the right, but it's not working. 
  When I commit the FragmentTransaction, I get <code>java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.</code> What is the child's parent, and why do I have to call removeView on it?</p>
  
  <p>I'm trying to load the fragment into a FrameLayout, but I've also just tried replacing another Fragment in the layout, and still get the error. </p>
  
  <p>Ideally I want the left fragment to take up the whole screen until a button is pressed that requires a fragment to come in from the left, but one problem at a time I suppose.</p>
  
  <h2>Code</h2>
  
  <h3>Main Activity</h3>
  
  <pre><code>public class FragmentExample2Activity extends Activity {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
      }
  }
  </code></pre>
  
  <h3>SelectorFragment (Left side)</h3>
  
  <pre><code>public class SelectorFragment extends Fragment {
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
          View fragment = inflater.inflate(R.layout.selector, container);
  
          Button button1 = (Button) fragment.findViewById(R.id.button1);
  
          button1.setOnClickListener(new OnClickListener(){
  
              @Override
              public void onClick(View v) {
  
                  getFragmentManager().beginTransaction().replace(R.id.detail_holder, new DetailsFragment(), "stuff").commit();
  
              }
  
          });
  
  
          return fragment;
      }
  }
  </code></pre>
  
  <h3>DetailsFragment</h3>
  
  <pre><code>public class DetailsFragment extends Fragment {
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
          return inflater.inflate(R.layout.details, container);
      }
  }
  </code></pre>
  
  <h3>main.xml</h3>
  
  
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      &gt;
  
      &lt;fragment class="com.coreno.testfragment.SelectorFragment"
          android:id="@+id/select"
          android:layout_width="match_parent"
          android:layout_height="match_parent" 
          android:layout_weight="1"
          /&gt;
  
      &lt;fragment class="com.coreno.testfragment.DetailsFragment"
          android:id="@+id/detail_holder"
          android:layout_width="match_parent"
          android:layout_height="match_parent" 
          android:layout_weight="2"
          /&gt;
      &lt;!-- 
      &lt;FrameLayout
          android:id="@+id/detail_holder" 
          android:layout_width="0px"
          android:layout_height="match_parent"
          android:layout_weight="2"
          /&gt;
   --&gt;
  &lt;/LinearLayout&gt;
  </code></pre>