title: How to implement Google Maps within a Fragment and use the GoogleMap object?
body: <p>How to implement GoogleMap in a Fragment?</p>
  <p>I'm trying to develop the same thing, but getMap() cannot be executed because SupportMapFragment (when executing the line (SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(‌​R.id.map) ) returns a null object.</p>
  <p>I've tried extending MapFragment and SupportMapFragment instead of Fragment, but they didn't work...</p>
  <p>I just don't want to create a new activity to show a map: I want to show a mere Map instead my main activity. Can I carry out my goal?</p>
  <p>I have a main activity which extends ActionBarActivity, who replaces fragments inside a container (depending on the user selection on the drawer, X or Y fragment is replaced and shown). I own 2 fragments working, but I cannot accomplish the goal of manipulating a GoogleMap object inside a Fragment.</p>
  <p>That's my mapa.xml</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;fragment
      xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:id=&quot;@+id/mapaPrincipal&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      class=&quot;com.google.android.gms.maps.SupportMapFragment&quot; /&gt;
  </code></pre>
  <p>That's my FragmentMapa class:</p>
  <pre><code>  import com.google.android.gms.maps.GoogleMap;
     import com.google.android.gms.maps.SupportMapFragment;
     import com.ingartek.bizkaimove.R;
  
     import android.app.Activity;
     import android.os.Bundle;
     import android.support.v4.app.Fragment;
     import android.view.LayoutInflater;
     import android.view.View;
     import android.view.ViewGroup;
  
   public class FragmentMapa extends Fragment {
  
  private GoogleMap mMapa;
      
  public FragmentMapa() {
      // TODO Auto-generated constructor stub
  }
  
  @Override
  public void onAttach (Activity activity) {
      super.onAttach(activity);
  }
  
  @Override
  public void onCreate (Bundle savedInstanceState) {
      super.onCreate(savedInstanceState); 
  }
  
  @Override
  public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      
      View rootView = inflater.inflate(R.layout.mapa, container, false);
      
      mMapa = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.mapaPrincipal)).getMap();      
      
      return rootView;
      
  }
  </code></pre>
  <p>The line highlighted is not working, because the SupportMapFragment object is null and therefore, getMap() crashes.</p>
  <p>Any help please? Thanks in advance.</p>
  <h1><strong>SOLUTION TO MY PROBLEM</strong></h1>
  <p>For anyone who faces a problem like mine: you shouldn't use getSupportFragmentManager(), instead you should use getChildFragmentManager(). This should remain that way:</p>
  <pre><code>@Override
  public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      
      View rootView = inflater.inflate(R.layout.mapa, container, false);
      
      setupMapIfNeeded();
      
      return rootView;
      
  }
  
  private void setupMapIfNeeded() {
  
      if( mMapa == null ){
          SupportMapFragment smf = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapaPrincipal);
      
          if( smf != null ){
              Toast.makeText(getActivity(), &quot;Let's go!!&quot;, Toast.LENGTH_SHORT).show();
              mMapa = smf.getMap();
          }else{
              Toast.makeText(getActivity(), &quot;SMF is null...&quot;, Toast.LENGTH_SHORT).show();
          }
          
          if( mMapa != null ){
              
              setupMap();
              
          }
      
      }
      
  }
  
  private void setupMap() {
      // TODO Auto-generated method stub
      Toast.makeText(getActivity(), &quot;Configure the map&quot;, Toast.LENGTH_SHORT).show();
  }
  </code></pre>