title: Google Maps MapFragment causing the app to crash
body: <p>I am fairly new to android programming but what I am trying to do seems fairly simple though. I have this setup with 4 different tabs in the action bar, the first should display a Fragment with the Google maps API and the others some other stuff i haven't done yet. Following some  tutorials online I managed to get the functionality I wanted for the tabs and get the map to appear. </p>
  
  <p>The app is working fine when changing from the initial tab with the map (tab 1) to any other, but when trying to come back it crashes. Weirdly enough when the map fragment is in one of the tabs it makes the second tab crash as well on some transitions, in the image bellow the transitions shown as blue arrows work fine, while the ones with yellow arrows make the app crash. </p>
  
  <p><a href="http://imageshack.com/a/img839/4796/kf7w.png" rel="nofollow">http://imageshack.com/a/img839/4796/kf7w.png</a></p>
  
  <p>If I change the first tab fragment for one of the others all the transitions work fine again, therefore I think the problem must indeed be with the map fragment.</p>
  
  <p>My code is as follows:</p>
  
  <p>MainActivity.java</p>
  
  <pre><code>public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
  private ViewPager viewPager;
  private TabsPagerAdapter mAdapter;
  private ActionBar actionBar;
  private String[] tabs = null;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      viewPager = (ViewPager) findViewById(R.id.pager);
      actionBar = getActionBar();
      mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
  
      viewPager.setAdapter(mAdapter);
      actionBar.setHomeButtonEnabled(false);
      actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  
      tabs = getResources().getStringArray(R.array.tab_names);
      for (String tab_name : tabs)
          actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
  
      /**
       * on swiping the viewpager make respective tab selected
       * */
      viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
  
          @Override
          public void onPageSelected(int position) {
              // on changing the page
              // make respected tab selected
              actionBar.setSelectedNavigationItem(position);
          }
  
          @Override
          public void onPageScrolled(int arg0, float arg1, int arg2) {
          }
  
          @Override
          public void onPageScrollStateChanged(int arg0) {
          }
      });
  }
  
  @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 void onTabReselected(Tab tab, FragmentTransaction ft) {
      // TODO Auto-generated method stub
  
  }
  
  @Override
  public void onTabSelected(Tab tab, FragmentTransaction ft) {
      viewPager.setCurrentItem(tab.getPosition());        
  }
  
  @Override
  public void onTabUnselected(Tab tab, FragmentTransaction ft) {
      // TODO Auto-generated method stub
  
  }
  
  }
  </code></pre>
  
  <p>TabsPagerAdapter.java</p>
  
  <pre><code>public class TabsPagerAdapter extends FragmentPagerAdapter {
  
  public TabsPagerAdapter(FragmentManager fm) {
      super(fm);
  }
  
  @Override
  public Fragment getItem(int index) {
      switch (index){
      case 0:
          return new GoogleMapsFragment();
      case 1:
          return new SampleFragment();
      case 2:
          return new ContactFragment();
      case 3:
          return new WhoFragment();
      default:
          Log.e("switch", "default");
      }
      return null;
  }
  
  @Override
  public int getCount() {
       // get item count - equal to number of tabs
      return 4;
  }
  
  }
  </code></pre>
  
  <p>GoogleMapsFragment.java</p>
  
  <pre><code>public class GoogleMapsFragment extends Fragment {
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
  
      View rootView = inflater.inflate(R.layout.activity_map, container, false);
  
      return rootView;
  }
  
  @Override
  public void onDestroyView() {
      super.onDestroyView();
      Fragment f = (Fragment) getFragmentManager().findFragmentById(R.id.map);        
      if (f != null) {
          getFragmentManager().beginTransaction().remove(f).commit();
      }
  }
  }
  </code></pre>
  
  <p>And the xml files:</p>
  
  <p>activity_main.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:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context=".MainActivity"
  
  android:background="#ffff0000"
  android:id="@+id/mainLayout"
  android:tag="@+id/mainLayoutTag" &gt;
  
  &lt;android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/pager"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:tag="pagerTag"&gt;
  &lt;/android.support.v4.view.ViewPager&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>activity_map.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"
  android:orientation="vertical"
  android:id="@+id/mapLayout"
  android:tag="mapLayoutTag" &gt;
  
  &lt;fragment android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.google.android.gms.maps.MapFragment"
        android:tag="gMaps"/&gt;
  
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>The other java classes are pretty much empty and the other xml files only contain the default RelativeLayout.</p>
  
  <p>Any suggestion as to what I could do to fix this?
  Thanks for your time reading all of that :)</p>
  
  <p>EDIT:
  Forgot to put the crash error:</p>
  
  <pre><code>02-10 23:42:01.723: E/AndroidRuntime(7221): Caused by: 
  java.lang.IllegalArgumentException: Binary XML file line #9: 
  Duplicate id 0x7f05000a, tag gMaps, or parent id 0x7f050009 with another 
  fragment for com.google.android.gms.maps.MapFragment
  </code></pre>