title: Android: trying to add a fragment to the backstack ("Blank activity" project)
body: <p>I have just created a new "Blank activity" project. And now I would like to add a fragment to the backstack when pressing the button that the "Blank activity" shows. The problem: the applicaction closes unexpectedly when pressing the button.</p>
  
  <p><a href="http://developer.android.com/intl/es/guide/components/fragments.html" rel="nofollow">Steps I have followed to create a fragment from an activity</a></p>
  
  <p><a href="http://developer.android.com/intl/es/training/implementing-navigation/temporal.html" rel="nofollow">Steps I have followed to add a fragment to the backstack</a> </p>
  
  <p>These are the only codes I added to the Blank Activity project:</p>
  
  <p>//MainActivity.java</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      context = this;
  
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
  
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
  
      FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
      fab.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
  
              ExampleFragment ef = new ExampleFragment();
  
              /////// This line closes the application /////////
              getFragmentManager().beginTransaction()
                      .add(65, ef)  //65 is random number
                       // Add this transaction to the back stack
                      .addToBackStack(null)
                      .commit();
  
              setContentView(R.layout.news_articles);
  
          }
      });
  
  }
  </code></pre>
  
  <p>This is the layout of the fragment:</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"&gt;
  
      &lt;fragment android:name="com.example.tirengarfio.myapplication.ExampleFragment"
          android:id="@+id/headlines_fragment"
          android:layout_weight="1"
          android:layout_width="0dp"
          android:layout_height="match_parent" /&gt;
  
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>ExampleFragment.java</p>
  
  <pre><code>package com.example.tirengarfio.myapplication;
  
  import android.app.Fragment;
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  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>