So I have an empty fragment that contains a map fragment. Whenever I try to activate the fragment containing the map, my app crashes and returns a null pointer error on this
Your class extends from Fragment, and you are using a Fragment declared in the layout, but nested fragment only work when they are added dynamically.
So, you have two options:
1) Extending from FragmentAcivity instead of Fragment and applying the proper methods.
2) Add the Fragment dynamically, as follow:
//ReturnVisitDetails
MapFragment mapFragment = new MapFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.rl_map_container, mapFragment).commit();
//Layout
--UPDATED--
A complete example to use Google Maps inside a Fragment:
//TestFragmentActivity
public class TestFragmentActivity extends android.support.v4.app.FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_fragment_activity);
}
}
//TestFragment
public class TestFragment extends android.support.v4.app.Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.test_fragment, container, false);
CustomMapFragment mapFragment = new CustomMapFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.map_container, mapFragment).commit();
return rootView;
}
}
//CustomMapFragment
public class CustomMapFragment extends com.google.android.gms.maps.SupportMapFragment {
private final LatLng HAMBURG = new LatLng(53.558, 9.927);
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
GoogleMap googleMap = getMap();
googleMap.addMarker(new MarkerOptions().position(HAMBURG).title("Hamburg"));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
}
//test_fragment_activity.xml
//test_fragment.xml