title: Android fragments - The specified child already has a parent. You must call removeView() on the child's parent first
body: <p>I am trying to create an app with a navigation drawer. It works well till I try to add another TextView above the list view in my navigation drawer fragment. Then it gives this exception when I try to setContentView in my HomeActivity: </p>
  
  <pre><code>java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
  </code></pre>
  
  <p>Here's all the relevant code: </p>
  
  <p>fragment_navigation_drawer.xml: <strong>(The fb_name textview is what I need to add.)</strong></p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal"&gt;
  
  &lt;TextView
      android:id="@+id/fb_name"
      android:layout_width="@dimen/navigation_drawer_width"
      android:layout_height="60dp"
      android:text="First Last"
      android:textColor="@android:color/white" /&gt;
  
  &lt;ListView
      android:id="@+id/nav_drawer"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#000000"
      android:choiceMode="singleChoice"
      android:divider="#ffcdcdcd"
      android:paddingTop="40dp"
      tools:context=".NavigationDrawerFragment" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>This is where I inflate the fragment -</p>
  
  <pre><code>@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      ViewGroup group = (ViewGroup) inflater.inflate(
              R.layout.fragment_navigation_drawer, container, false);
      mDrawerListView = (ListView) group.findViewById(R.id.nav_drawer);
      mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
              selectItem(position);
          }
      });
  
      ArrayList&lt;DrawerItem&gt; drawerItems = new ArrayList&lt;DrawerItem&gt;();
  
      DrawerItem drawerItem1 = new DrawerItem("Home", R.drawable.home);
      DrawerItem drawerItem2 = new DrawerItem("Start a Contest", R.drawable.hand_point);
      DrawerItem drawerItem3 = new DrawerItem("All Contests", R.drawable.briefcase);
      DrawerItem drawerItem4 = new DrawerItem("My Contests", R.drawable.binoculars);
      DrawerItem drawerItem5 = new DrawerItem("Login", R.drawable.home);
  
      drawerItems.add(drawerItem1);
      drawerItems.add(drawerItem2);
      drawerItems.add(drawerItem3);
      drawerItems.add(drawerItem4);
      drawerItems.add(drawerItem5);
  
      DrawerAdapter adapter = new DrawerAdapter(getActivity(), drawerItems);
      mDrawerListView.setAdapter(adapter);
      mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
      return mDrawerListView;
  }
  </code></pre>
  
  <p>This is the activity onCreate() where the setContentView is throwing the exception -</p>
  
  <pre><code>    @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_home); //This is where the exception occurs
  
      mNavigationDrawerFragment = (NavigationDrawerFragment)
              getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
      mTitle = getTitle();
  
      // Set up the drawer.
      mNavigationDrawerFragment.setUp(
              R.id.navigation_drawer,
              (DrawerLayout) findViewById(R.id.drawer_layout));
  }
  </code></pre>
  
  <p>This is where my DrawerLayout is -</p>
  
  <pre><code>&lt;!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. --&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>&lt;!-- As the main content view, the view below consumes the entire
       space available using match_parent in both dimensions. --&gt;
  &lt;FrameLayout android:id="@+id/container" android:layout_width="match_parent"
      android:layout_height="match_parent" /&gt;
  
  &lt;!-- android:layout_gravity="start" tells DrawerLayout to treat
       this as a sliding drawer on the left side for left-to-right
       languages and on the right side for right-to-left languages.
       If you're not building against API 17 or higher, use
       android:layout_gravity="left" instead. --&gt;
  &lt;!-- The drawer is given a fixed width in dp and extends the full height of
       the container. --&gt;
  &lt;fragment android:id="@+id/navigation_drawer"
      android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
      android:layout_gravity="start"
      android:name="com.labs.thinkdifferent.voice.NavigationDrawerFragment"
      tools:layout="@layout/fragment_navigation_drawer" /&gt;
  
  &lt;/android.support.v4.widget.DrawerLayout&gt;
  </code></pre>