/ android

Retrieve SupportMapFragment on Android Fragment

Today, I implement a sample to show Google Maps on Android Fragment. I have an activity (MainActivity), two fragments contained in MainActivity is InfoFragment and MapFragment. In MapFragment, I attach a MapFragment in layout file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
  
  <fragment android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</LinearLayout>

Setup map implemented in MapFragment.java

/** * Created by haint on 7/13/2015. */
public class MapFragment extends Fragment {
 private GoogleMap mMap;
 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  return inflater.inflate(R.layout.fragment_map, container, false);
 }
 @Override public void onActivityCreated(Bundle savedInstanceState) {
  super.onActivityCreated(savedInstanceState);
  setUpMapIfNeeded();
 }
 @Override public void onResume() {
  super.onResume();
  setUpMapIfNeeded();
 }
 private void setUpMapIfNeeded() {
  // Do a null check to confirm that we have not already instantiated the map.
  if (mMap == null) {
   // Try to obtain the map from the SupportMapFragment.
   mMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
   // Check if we were successful in obtaining the map.
   if (mMap != null) {
    setUpMap();
   }
  }
 }

 /** * This is where we can add markers or lines, add listeners or move the camera. In this case, we * just add a marker near Africa. * <p/> * This should only be called once and when we are sure that [email protected] #mMap} is not null. */
 private void setUpMap() {
  mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
 }
}

I encountered an error when show Mapfragment with logged:

07-14 10:44:17.303 22695-22695/net.awpspace.demogooglemaps
E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: net.awpspace.demogooglemaps, PID: 22695 java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()' on a null object reference

I searched reason of the error and had a solution for this:

The layout containing the map fragment is the layout for MapFragment. In that case, the SupportMapFragment is a child fragment of MapFragment. When I attempt to retrieve it, I’m using the Activity’s FragmentManager then encounter error. Solution for this:

mMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

INSTEAD BY:

mMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();

Now it should work normally.