title: Add fragment programmatically on button click
body: <p>I know this is kind of a noob question, but I come from php and javascript, and I've just started learning java and android. I've read the dev documentation on fragments, and I'm trying to add a fragment on button click. App starts, but dies on button click. I don't know if my logic is wrong here? (Adding fragments directly in xml works).</p>
  
  <p>Fragment class:</p>
  
  <pre><code>public class ExampleFragment extends Fragment {
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          // Inflate the layout for this fragment
          return inflater.inflate(R.layout.example_fragment, container, false);
      }
  
  
  }
  </code></pre>
  
  <p>Fragment xml:</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:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@+id/seekBar1"
          android:layout_centerHorizontal="true"
          android:layout_marginTop="17dp"
          android:text="Button" /&gt;
  
      &lt;EditText
          android:id="@+id/editText1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentTop="true"
          android:layout_centerHorizontal="true"
          android:layout_marginTop="16dp"
          android:ems="10"
          android:inputType="text" &gt;
          &lt;requestFocus /&gt;
      &lt;/EditText&gt;
  
      &lt;SeekBar
          android:id="@+id/seekBar1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_below="@+id/editText1"
          android:layout_marginTop="14dp" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Activity class:</p>
  
  <pre><code>public class MainActivity extends Activity implements OnClickListener{
      Button addFragment;
      android.app.FragmentManager fragmentManager = getFragmentManager();
      FragmentTransaction transaction = fragmentManager.beginTransaction();
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          initialize();
      }
  
      private void initialize() 
      {
          addFragment = (Button) findViewById(R.id.bAddF);
          addFragment.setOnClickListener(this);
      }
  
      @Override
      public void onClick(View v) {
          // TODO Auto-generated method stub
              ExampleFragment fragment = new ExampleFragment();
              transaction.add(R.id.fragment_holder, fragment);
              transaction.commit();
  
      }
  </code></pre>
  
  <p>}</p>
  
  <p>Activity xml:</p>
  
  <pre><code>&lt;RelativeLayout 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"
      tools:context="${packageName}.${activityClass}" &gt;
  
      &lt;TextView
          android:id="@+id/tvHello"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/hello_world" /&gt;
      &lt;Button 
          android:id="@+id/bAddF"
          android:layout_below="@id/tvHello"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Add Fragment"/&gt;
  
      &lt;View
          android:id="@+id/fragment_holder"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_below="@id/bAddF"&gt;
      &lt;/View&gt;
  
  
  &lt;/RelativeLayout&gt;
  </code></pre>