title: Error swapping / adding an XML Fragment
body: <p>I have been struggling with the Android 3.0 SDK's fragments concept for a few days now, and thought I'd try StackOverflow....</p>
  
  <p>I have a simple (proof on concept) app:</p>
  
  <p>Here is my main layout:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"&gt;
      &lt;RelativeLayout android:id="@+id/relativeLayout1" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="200dp"&gt;
  &lt;fragment android:name="com.owentech.simplefragmentswap.staticfragment"
              android:id="@+id/fragment1" android:layout_weight="1"
              android:layout_height="wrap_content" android:layout_width="fill_parent"/&gt;
  &lt;/RelativeLayout&gt;
  &lt;FrameLayout android:id="@+id/frameLayout1" android:layout_height="wrap_content"      android:layout_alignParentTop="true" android:layout_width="match_parent"    android:layout_above="@+id/relativeLayout1"&gt;&lt;/FrameLayout&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>I display one fragment (staticfragment) directly in the XML, this works fine.</p>
  
  <p>With the framelayout I wanted to add an XML fragment (fragment1) on create.</p>
  
  <p>Here is fragment1:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"&gt;
    &lt;Button android:layout_height="wrap_content" android:id="@+id/button1" android:layout_alignParentTop="true" android:layout_width="match_parent" android:text="Fragment 1"&gt;&lt;/Button&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Here is my mainactivity:</p>
  
  <pre><code>import android.app.Activity;
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentActivity;
  import android.support.v4.app.FragmentTransaction;
  
  public class mainActivity extends FragmentActivity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
   // Create new fragment and transaction
      Fragment newFragment = new fragment1();
      FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  
      // Replace whatever is in the fragment_container view with this fragment,
      // and add the transaction to the back stack
      //transaction.replace(R.id.frameLayout1, newFragment);
      transaction.add(R.id.frameLayout1, newFragment);
      transaction.addToBackStack(null);
  
      // Commit the transaction
      transaction.commit();
  
  }
  }
  </code></pre>
  
  <p>This causes a force close on start.</p>
  
  <p>Here is the logcat:</p>
  
  <pre><code>05-18 09:49:23.073: INFO/ActivityManager(71): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.owentech.simplefragmentswap/.mainActivity }
  05-18 09:49:23.313: INFO/ActivityManager(71): Start proc com.owentech.simplefragmentswap for activity com.owentech.simplefragmentswap/.mainActivity: pid=451 uid=10036 gids={1015}
  05-18 09:49:23.324: DEBUG/AndroidRuntime(445): Shutting down VM
  05-18 09:49:23.333: DEBUG/dalvikvm(445): Debugger has detached; object registry had 1 entries
  05-18 09:49:23.404: INFO/AndroidRuntime(445): NOTE: attach of thread 'Binder Thread #3' failed
  05-18 09:49:24.433: DEBUG/AndroidRuntime(451): Shutting down VM
  05-18 09:49:24.433: WARN/dalvikvm(451): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
  05-18 09:49:24.473: ERROR/AndroidRuntime(451): FATAL EXCEPTION: main
  **05-18 09:49:24.473: ERROR/AndroidRuntime(451): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.owentech.simplefragmentswap/com.owentech.simplefragmentswap.mainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.**
  05-18 09:49:24.473: ERROR/AndroidRuntime(451):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
  05-18 09:49:24.473: ERROR/AndroidRuntime(451):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
  05-18 09:49:24.473: ERROR/AndroidRuntime(451):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
  05-18 09:49:24.473: ERROR/AndroidRuntime(451):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
  05-18 09:49:24.473: ERROR/AndroidRuntime(451):     at android.os.Handler.dispatchMessage(Handler.java:99)
  05-18 09:49:24.473: ERROR/AndroidRuntime(451):     at android.os.Looper.loop(Looper.java:123)
  05-18 09:49:24.473: ERROR/AndroidRuntime(451):     at android.app.ActivityThread.main(ActivityThread.java:4627)
  05-18 09:49:24.473: ERROR/AndroidRuntime(451):     at java.lang.reflect.Method.invokeNative(Native Method)
  05-18 09:49:24.473: ERROR/AndroidRuntime(451):     at java.lang.reflect.Method.invoke(Method.java:521)
  05-18 09:49:24.473: ERROR/AndroidRuntime(451):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
  05-18 09:49:24.473: ERROR/AndroidRuntime(451):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
  05-18 09:49:24.473: ERROR/AndroidRuntime(451):     at dalvik.system.NativeStart.main(Native Method)
  05-18 09:49:24.473: ERROR/AndroidRuntime(451): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
  05-18 09:49:24.473: ERROR/AndroidRuntime(451):     at android.view.ViewGroup.addViewInner(ViewGroup.java:1970)
  05-18 09:49:24.473: ERROR/AndroidRuntime(451):     at android.view.ViewGroup.addView(ViewGroup.java:1865)
  05-18 09:49:24.473: ERROR/AndroidRuntime(451):     at android.view.ViewGroup.addView(ViewGroup.java:1822)
  05-18 09:49:24.473: ERROR/AndroidRuntime(451):     at android.view.ViewGroup.addView(ViewGroup.java:1802)
  05-18 09:49:24.473: ERROR/AndroidRuntime(451):     at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
  05-18 09:49:24.473: ERROR/AndroidRuntime(451):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:743)
  05-18 09:49:24.473: ERROR/AndroidRuntime(451):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:933)
  05-18 09:49:24.473: ERROR/AndroidRuntime(451):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:916)
  05-18 09:49:24.473: ERROR/AndroidRuntime(451):     at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1587)
  05-18 09:49:24.473: ERROR/AndroidRuntime(451):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:500)
  05-18 09:49:24.473: ERROR/AndroidRuntime(451):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
  05-18 09:49:24.473: ERROR/AndroidRuntime(451):     at android.app.Activity.performStart(Activity.java:3781)
  05-18 09:49:24.473: ERROR/AndroidRuntime(451):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
  05-18 09:49:24.473: ERROR/AndroidRuntime(451):     ... 11 more
  05-18 09:49:24.494: WARN/ActivityManager(71):   Force finishing activity com.owentech.simplefragmentswap/.mainActivity
  </code></pre>
  
  <p>Does anybody know what I am doing wrong, as I have taken the code directly from developer.android.com</p>
  
  <p>Thanks</p>