title: How to add map to a tabbed activity (Android)
body: <p>I have a tabbed activity which uses a SectionsPageAdapter. There are two tabs, each uses a different fragment, one of which is a google map. When I attempt to get the <code>SupportMapFragment</code> from the MapFragment it returns null.</p>
  
  <pre><code>SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
                  .findFragmentById(R.id.map);
  </code></pre>
  
  <p>I have also tried:</p>
  
  <pre><code>SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager()
                  .findFragmentById(R.id.map);
  </code></pre>
  
  <p>If I create a new GoogleMapsActivity it works just fine, that confirms that it has nothing to do with my key. the fragment with id <code>map</code> exists.</p>
  
  <p>MainActivity.java</p>
  
  <pre><code>package com.example.frias19o.trackthem2;
  
  import android.support.design.widget.TabLayout;
  import android.support.design.widget.FloatingActionButton;
  import android.support.design.widget.Snackbar;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
  
  import android.support.v4.view.ViewPager;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  
  public class MainActivity extends AppCompatActivity {
  
      private SectionsPagerAdapter mSectionsPagerAdapter;
  
      private ViewPager mViewPager;
  
      @Override
      protected void onCreate(Bundle savedInstanceState)  {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
          // Create the adapter that will return a fragment for each of the three
          // primary sections of the activity.
          mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
  
          // Set up the ViewPager with the sections adapter.
          mViewPager = (ViewPager) findViewById(R.id.container);
          mViewPager.setAdapter(mSectionsPagerAdapter);
  
          TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
          tabLayout.setupWithViewPager(mViewPager);
  
          FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
          fab.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                          .setAction("Action", null).show();
              }
          });
      }
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.menu_map, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          //noinspection SimplifiableIfStatement
          if (id == R.id.action_settings) {
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
  
  }
  </code></pre>
  
  <p>MapFragment.java</p>
  
  <pre><code>package com.example.frias19o.trackthem2;
  
  import android.content.Intent;
  import android.os.Bundle;
  import android.provider.Settings;
  import android.support.annotation.NonNull;
  import android.support.annotation.Nullable;
  import android.support.v4.app.Fragment;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Toast;
  
  import com.google.android.gms.common.ConnectionResult;
  import com.google.android.gms.common.api.GoogleApiClient;
  import com.google.android.gms.location.LocationServices;
  import com.google.android.gms.maps.CameraUpdateFactory;
  import com.google.android.gms.maps.GoogleMap;
  import com.google.android.gms.maps.OnMapReadyCallback;
  import com.google.android.gms.maps.SupportMapFragment;
  import com.google.android.gms.maps.model.LatLng;
  import com.google.android.gms.maps.model.MarkerOptions;
  
  import org.json.JSONException;
  import org.json.JSONObject;
  
  /********************************************************************
   * Fragment for Map
   *********************************************************************/
  public class MapFragment extends Fragment implements OnMapReadyCallback {
      private static final String LOGTAG = "MapFragment";
      private static final String ARG_SECTION_NUMBER = "section_number";
      public static GoogleApiClient mGoogleApiClient;
      private GoogleMap mMap;
  
      public MapFragment() {
      }
  
      public static MapFragment newInstance(int sectionNumber) {
          MapFragment fragment = new MapFragment();
          Bundle args = new Bundle();
          args.putInt(ARG_SECTION_NUMBER, sectionNumber);
          fragment.setArguments(args);
          return fragment;
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View rootView;
          Toast.makeText(getContext(), "fragment_map", Toast.LENGTH_SHORT).show();
          rootView = inflater.inflate(R.layout.activity_maps, container, false);
  
          return rootView;
      }
  
      @Override
      public void onActivityCreated(@Nullable Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
  
          SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager()
                  .findFragmentById(R.id.map);
          mapFragment.getMapAsync(this); // it brakes here because mapFragment is null
      }
  
  
      @Override
      public void onMapReady(GoogleMap googleMap) {
          mMap = googleMap;
          Log.v(LOGTAG, "map is ready");
  
          LatLng sydney = new LatLng(-34, 151);
          mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
          mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
          Log.v(LOGTAG, "Added Sydney");
      }
  }
  </code></pre>
  
  <p>SectionsPagerAdapter.java</p>
  
  <pre><code>package com.example.frias19o.trackthem2;
  
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentPagerAdapter;
  
  /***********************************************************************
   * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
   * one of the sections/tabs/pages.
   **********************************************************************/
  public class SectionsPagerAdapter extends FragmentPagerAdapter {
  
      public SectionsPagerAdapter(FragmentManager fm) {
          super(fm);
      }
  
      @Override
      public Fragment getItem(int position) {
          // getItem is called to instantiate the fragment for the given page.
          // Return a ClusterFragment (defined as a static inner class below).
  
          Fragment fragment = null;
  
          switch (position) {
              case 0:
                  fragment = MapFragment.newInstance(position + 1);
                  break;
              case 1:
                  fragment = ClusterFragment.newInstance(position + 1);
                  break;
              default:
                  break;
          }
  
          return fragment;
      }
  
      @Override
      public int getCount() {
          // Show 2 total pages.
          return 2;
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          switch (position) {
              case 0:
                  return "SECTION 1";
              case 1:
                  return "SECTION 2";
          }
          return null;
      }
  }
  </code></pre>