title: Fragment with Map crashes app when back button is pressed or when orientation changes
body: <p>I have an <code>Activity</code> that loads 2 tabs in the <code>ActionBar</code>. Tab 1 loads a <code>Fragment</code> that will will inflate a <code>WebView</code>, while Tab 2 loads a <code>Fragment</code> with a map. Initially, I have the problem of changing tabs that causes the app to crash. I have followed the instructions <a href="https://stackoverflow.com/questions/14074625/mapfragment-in-action-bar-tabs/14466954#14466954">here</a> and implemented <code>onDestroyView()</code>. </p>
  
  <p>The problem now is that if I press the back button from Tab 1 (<code>WebView</code>), it exits the <code>Activity</code> properly. But if I do it on Tab 2 (map), the app crashes. The exact same scenario applies to when I change the orientation. I am convinced that it has something to do with the <code>onDestroyView()</code>, but I am not sure what it is.</p>
  
  <p>I have even attempted to <code>Override</code> the back button, but nothing works.</p>
  
  <p>Here is my code to provide some context:</p>
  
  <p>The Activity Class:</p>
  
  <pre><code>public class MyActivity extends Activity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      //Displaying the tabs by calling ActionBar
      ActionBar actionBar = getActionBar();
      actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  
      //Details Tab
      String label1 = getResources().getString(R.string.details);
      Tab tab = actionBar.newTab();
      tab.setText(label1);
      TabListener&lt;DetailsFragment&gt; t1 = new TabListener&lt;DetailsFragment&gt;(this, label1, DetailsFragment.class);
      tab.setTabListener(t1);
      actionBar.addTab(tab);
  
      //Map Tab
      String label2 = getResources().getString(R.string.map);
      tab = actionBar.newTab();
      tab.setText(label2);
      TabListener&lt;MapFragment&gt; t2 = new TabListener&lt;MapFragment&gt;(this, label2, MapFragment.class);
      tab.setTabListener(t2);
      actionBar.addTab(tab);
  }
  
  private class TabListener&lt;T extends Fragment&gt; implements ActionBar.TabListener {
      private Fragment mFragment;
      private final Activity mActivity;
      private final String mTag;
      private final Class&lt;T&gt; mClass;
  
      /**
       * Constructor used each time a new tab is created.
       * 
       * @param activity
       *            The host Activity, used to instantiate the fragment
       * @param tag
       *            The identifier tag for the fragment
       * @param clz
       *            The fragment's Class, used to instantiate the fragment
       */
      public TabListener(Activity activity, String tag, Class&lt;T&gt; clz) {
          mActivity = activity;
          mTag = tag;
          mClass = clz;
      }
  
      public void onTabSelected(Tab tab, FragmentTransaction ft) {
          // Check if the fragment is already initialized
          if (mFragment == null) {
              // If not, instantiate and add it to the activity
              mFragment = Fragment.instantiate(mActivity, mClass.getName());
              ft.add(android.R.id.content, mFragment, mTag);
          } else {
              // If it exists, simply attach it in order to show it
              ft.attach(mFragment);
  
          }
      }
  
      public void onTabUnselected(Tab tab, FragmentTransaction ft) {
          if (mFragment != null) {
              // Detach the fragment, because another one is being attached
              ft.detach(mFragment);
          }
      }
  
      public void onTabReselected(Tab tab, FragmentTransaction ft) {
          // User selected the already selected tab. Usually do nothing.
      }
  }
  
  @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;
  } }
  </code></pre>
  
  <p>DetailsFragment:</p>
  
  <pre><code>public class DetailsFragment extends Fragment {
  
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
      View v = inflater.inflate(R.layout.morning, container, false);
      WebView webview = (WebView) v.findViewById(R.id.details);
      webview.loadUrl("file:///android_asset/pools/details.html");
      return v;
  } }
  </code></pre>
  
  <p>MapFragment:</p>
  
  <pre><code>public class MapFragment extends Fragment {
  
  static final LatLng mapLatLng = new LatLng("some numbers", "some numbers");
  private GoogleMap map;
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
      View v = inflater.inflate(R.layout.map, container, false);
      map = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
      Marker amksc = map.addMarker(new MarkerOptions().position(mapLatLng).title("Map"));
  
      map.moveCamera(CameraUpdateFactory.newLatLngZoom(mapLatLng, 18)); 
      return v;
  }
  
  
  @Override
  public void onDestroyView() {
      super.onDestroyView();
      MapFragment destroyMe = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
      if (destroyMe != null) {
          getFragmentManager().beginTransaction().remove(destroyMe).commit();
      }
  } }
  </code></pre>
  
  <p>Thanks for reading such a long post. Any help is greatly appreciated. I will be away for awhile without my laptop (15 days to be exact), so I may not be able to respond and test out your answer, but rest assured that I will =)</p>
  
  <p>Thanks in advance!</p>
  
  <p>EDITED to include Error Log (Not sure how to format it)</p>
  
  <pre><code>04-25 11:27:54.333: E/AndroidRuntime(21779): FATAL EXCEPTION: main
  
  04-25 11:27:54.333: E/AndroidRuntime(21779): java.lang.NullPointerException
  
  04-25 11:27:54.333: E/AndroidRuntime(21779):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1380)
  
  04-25 11:27:54.333: E/AndroidRuntime(21779):    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:430)
  
  04-25 11:27:54.333: E/AndroidRuntime(21779):    at android.os.Handler.handleCallback(Handler.java:615)
  
  04-25 11:27:54.333: E/AndroidRuntime(21779):    at android.os.Handler.dispatchMessage(Handler.java:92)
  
  04-25 11:27:54.333: E/AndroidRuntime(21779):    at android.os.Looper.loop(Looper.java:213)
  
  04-25 11:27:54.333: E/AndroidRuntime(21779):    at android.app.ActivityThread.main(ActivityThread.java:4786)
  
  04-25 11:27:54.333: E/AndroidRuntime(21779):    at java.lang.reflect.Method.invokeNative(Native Method)
  
  04-25 11:27:54.333: E/AndroidRuntime(21779):    at java.lang.reflect.Method.invoke(Method.java:511)
  
  04-25 11:27:54.333: E/AndroidRuntime(21779):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
  
  04-25 11:27:54.333: E/AndroidRuntime(21779):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
  
  04-25 11:27:54.333: E/AndroidRuntime(21779):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>