Neutrino Neutrino - 2 years ago 676
Android Question

mapFragment.getMapAsync(this) - NullPointerException

I am using
version of Google Maps services. When trying to call the below I get
java.lang.NullPointerException: Attempt to invoke virtual method 'void' on a null object reference

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager()
mapFragment.getMapAsync(this); //error

return inflater.inflate(R.layout.fragment_example_map, container, false);

fragment_example_map.xml file:

<FrameLayout xmlns:android=""

<fragment xmlns:android=""

Answer Source

You are attempting to find a fragment before it exists. You indicate that the layout that has the fragment is fragment_example_map.xml. However, you are trying to find the map fragment before you inflate that layout file. This will not work.

Beyond that, you appear to be trying to get at the map fragment from inside another fragment, in that fragment's onCreateView() method. I do not know why you are nesting fragments here, as it seems like it will make your code more complex and more fragile for no obvious benefit.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download