title: Android Fragments change
body: <p>I use Android Studio to build android app.</p>
  
  <p>My problem is: when fragment change, app crash.</p>
  
  <p>Console output
  [Console]</p>
  
  <pre><code>01-06 18:35:21.952  27756-27756/pl.example.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
      Process: pl.example.app, PID: 27756
      java.lang.IllegalArgumentException: No view found for id 0x7f07003d (pl.example.app:id/fragment_next) for fragment FragmentNext{4201c798 #1 id=0x7f07003d}
              at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
              at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
              at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
              at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
              at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
              at android.os.Handler.handleCallback(Handler.java:733)
              at android.os.Handler.dispatchMessage(Handler.java:95)
              at android.os.Looper.loop(Looper.java:136)
              at android.app.ActivityThread.main(ActivityThread.java:5017)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:515)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
              at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Container layout code
  [container.xml]</p>
  
  <pre><code>&lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/container"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      tools:context="pl.example.app.MainActivity"
      tools:ignore="MergeRootFrame" &gt;
  
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p>Fragment activity layout code
  [fragment_activity.xml]</p>
  
  <pre><code>&lt;FrameLayout 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"
      tools:context="pl.example.app.MainActivity$PlaceholderFragment"&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Button"
          android:onClick="onButtonClick"
          android:layout_gravity="center"/&gt;
  
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p>Main activity code
  [MainActivity.java]</p>
  
  <pre><code>package pl.example.app;
  
  import android.support.v4.app.FragmentActivity;
  import android.support.v4.app.Fragment;
  import android.os.Bundle;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentTransaction;
  import android.view.LayoutInflater;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.ViewGroup;
  
  public class MainActivity extends FragmentActivity {
  
      private FragmentManager fragmentManager = null;
      private FragmentTransaction fragmentTransaction = null;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.container);
  
          if (savedInstanceState == null) {
              fragmentManager = getSupportFragmentManager();
              fragmentTransaction = fragmentManager.beginTransaction();
              fragmentTransaction.add(R.id.container, new Container());
              fragmentTransaction.commit();
          }
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
  
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, 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();
          if (id == R.id.action_settings) {
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
      public static class Container extends Fragment {
  
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
              return inflater.inflate(R.layout.fragment_activity, container, false);
          }
  
      }
  
      public void onButtonClick(View view) {
          changefragment(R.id.fragment_next, new FragmentNext());
      }
  
      private void changefragment(int fragmentID, Fragment fragment) {
          fragmentTransaction = fragmentManager.beginTransaction();
          fragmentTransaction.replace(fragmentID, fragment);
          fragmentTransaction.commit();
      }
  }
  </code></pre>
  
  <p>Fragment next layout code
  [fragment_next.xml]</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/fragment_next"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      tools:context="pl.example.app.MainActivity"
      tools:ignore="MergeRootFrame"&gt;
  
      &lt;TextView
          android:text="Fragment Next"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center"/&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Fragment code [FragmentNext.java]</p>
  
  <pre><code>package pl.example.app;
  
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  /**
   * Created by Nerus on 06.01.14.
   */
  public class FragmentNext extends Fragment {
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          return inflater.inflate(R.layout.fragment_next, container, false);
      }
  
  }
  </code></pre>