title: App Crashes when replacing Android Fragments, android studio
body: <p>I am new to android programming and I am trying to write a simple application that switches between two Fragments. I started a new project with a blank activity template that already included a Fragment. I think this could be where I am going wrong, but I am lost. Every time I launch and click the button the app crashes. It throws a must implement OnFragmentInteractionListener error from MyFragment2's onAttach method. If I comment that method out it add's the new Fragment on top of the old one. I have been through just about every tutorial I can find. I do not see where my code is going wrong. I'm sure it is something easy and I am going to feel pretty stupid when I finally figure it out. Any help would be appreciated. Thanks in advance. </p>
  
  <p>My main activity:</p>
  
  <pre><code>package com.example.misanthropic.fragments;
  
  import android.app.Activity;
  import android.app.ActionBar;
  import android.app.Fragment;
  import android.app.FragmentManager;
  import android.app.FragmentTransaction;
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.ViewGroup;
  import android.os.Build;
  
  
  
  public class Main extends Activity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      if (savedInstanceState == null) {
          getFragmentManager().beginTransaction()
                  .add(R.id.container, new PlaceholderFragment())
                  .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
      // matically 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 void buttonClicked(View v){
  
      FragmentManager fragmentManager = getFragmentManager();
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  
      Fragment2 myFragment = new Fragment2();
      fragmentTransaction.replace(R.id.fragment_main, myFragment);
      fragmentTransaction.addToBackStack(null);
      fragmentTransaction.commit();
  }
  
  /**
   * A placeholder fragment containing a simple view.
   */
  public static class PlaceholderFragment extends Fragment {
  
      public PlaceholderFragment() {
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment_main, container, false);
          return rootView;
      }
  }
  }
  </code></pre>
  
  <p>The MyFragment2 I would like to swap in:</p>
  
  <pre><code>package com.example.misanthropic.fragments;
  
  import android.app.Activity;
  import android.net.Uri;
  import android.os.Bundle;
  import android.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  /**
   * A simple {@link Fragment} subclass.
   * Activities that contain this fragment must implement the
   * {@link Fragment2.OnFragmentInteractionListener} interface
   * to handle interaction events.
   * Use the {@link Fragment2#newInstance} factory method to
   * create an instance of this fragment.
   *
   */
      public class Fragment2 extends Fragment {
      // TODO: Rename parameter arguments, choose names that match
      // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
      private static final String ARG_PARAM1 = "param1";
      private static final String ARG_PARAM2 = "param2";
  
      // TODO: Rename and change types of parameters
      private String mParam1;
      private String mParam2;
  
      private OnFragmentInteractionListener mListener;
  
      /**
       * Use this factory method to create a new instance of
       * this fragment using the provided parameters.
       *
       * @param param1 Parameter 1.
       * @param param2 Parameter 2.
       * @return A new instance of fragment Fragment2.
       */
  // TODO: Rename and change types and number of parameters
  public static Fragment2 newInstance(String param1, String param2) {
      Fragment2 fragment = new Fragment2();
      Bundle args = new Bundle();
      args.putString(ARG_PARAM1, param1);
      args.putString(ARG_PARAM2, param2);
      fragment.setArguments(args);
      return fragment;
  }
  public Fragment2() {
      // Required empty public constructor
  }
  
  
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      // Inflate the layout for this fragment
      return inflater.inflate(R.layout.fragment_fragment2, container, false);
  }
  
  // TODO: Rename method, update argument and hook method into UI event
  public void onButtonPressed(Uri uri) {
      if (mListener != null) {
          mListener.onFragmentInteraction(uri);
      }
  }
  
  @Override
  public void onAttach(Activity activity) {
      super.onAttach(activity);
      try {
          mListener = (OnFragmentInteractionListener) activity;
      } catch (ClassCastException e) {
          throw new ClassCastException(activity.toString()
                  + " must implement OnFragmentInteractionListener");
      }
  }
  
  @Override
  public void onDetach() {
      super.onDetach();
      mListener = null;
  }
  
  /**
   * This interface must be implemented by activities that contain this
   * fragment to allow an interaction in this fragment to be communicated
   * to the activity and potentially other fragments contained in that
   * activity.
   * &lt;p&gt;
   * See the Android Training lesson &lt;a href=
   * "http://developer.android.com/training/basics/fragments/communicating.html"
   * &gt;Communicating with Other Fragments&lt;/a&gt; for more information.
   */
  public interface OnFragmentInteractionListener {
      // TODO: Update argument type and name
      public void onFragmentInteraction(Uri uri);
  }
  
  }
  </code></pre>
  
  <p>My main 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="match_parent"
      android:layout_height="match_parent"
      tools:context=".Main"
      tools:ignore="MergeRootFrame" /&gt;
  </code></pre>
  
  <p>My Initial Fragment 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"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:paddingBottom="@dimen/activity_vertical_margin"
      tools:context=".Main$PlaceholderFragment"
      android:id="@+id/fragment_main"&gt;
  
      &lt;TextView
          android:text="@string/hello_world"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/textView" /&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="New Button"
          android:id="@+id/button"
          android:layout_below="@+id/textView"
          android:layout_centerHorizontal="true"
          android:layout_marginTop="165dp"
          android:onClick="buttonClicked" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>The replacement Fragment 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="match_parent"
      android:layout_height="match_parent"
      tools:context="com.example.misanthropic.fragments.Fragment2"&gt;
  
      &lt;!-- TODO: Update blank fragment layout --&gt;
      &lt;TextView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:text="@string/hello_blank_fragment" /&gt;
  
  &lt;/FrameLayout&gt;
  </code></pre>