I'm trying to load SupportMapFragment dynamically in a fragment, here is my onCreateView() method:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View contentView = inflater.inflate(R.layout.frag_map_layout, null);
shopDtos=ShopListFragment.shopDtos;
fragment =SupportMapFragment.newInstance();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.map_content, fragment);
ft.commit();
map = fragment.getMap();
mapMarker = map.addMarker(new MarkerOptions().position(new LatLng(0, 0))
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.maps_pin)));
return contentView;
}
Unfortunately, I get the GoogleMap map as null
.
Any suggestions on to how create a mapfragment dynamically?
Just try using the following code. It worked for me.
An up to date solution for this problem is to use getMapAsync (OnMapReadyCallback callback) instead.
map takes some time to load, so you need to run your code in handler -->
There's a bunch of duplicate questions that go around the same topic... I can't seem to figure out how to flag it as a duplicate and then put one answer on it...
Here's the link to the other one: Android SupportMapFragment.getMap() returns null
And the potential answer posted there as well:
It happened with me once in a new purchased device with Google play services not yet installed /updated. With other notes mentioned of course also consider this one.
Try getChildFragmentManager() in Fragment.