Skip to content Skip to sidebar Skip to footer

Nullpointerexception When Trying To Place Marker On Google Map V2

when I try to place Marker on google map with placeMarker method I get NullPointerException NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources an

Solution 1:

After 3 Days Searching and Working on it here is what finally worked.the real problem is googlemap is null which means map is not loaded.I used try and catch but You should never just catch and exception and do nothing. after Log the exceptions, check the logs I found this:

com.google.android.gms.maps.MapFragment.getMap()' on a null object reference

First I tried This link Google Map returning nullpointerexception Google Maps Android V2

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

Then getChildFragmentManager become red indicating getChildFragmentManager () cannot be resolved or cannot be referenced.using getSupportFragmentManager() instead as described in this link (getChildFragmentManager () cannot be resolved or cannot be referenced) finally solved the problem and map has been loaded

googlemap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapfragment)).getMap();

Note that if your class extends from AppCompatActivitythen You need to use getSupportFragmentManager() because getChildFragmentManager() is a method of a Fragment not accessible fromAppCompatActivity

Solution 2:

This is because google map object is cleared after if statement . Remove googlemap.clear() and try.

Solution 3:

Your map doesn't initialize or not to get your position. Check location sensor is active or not. If not, please active it. You can try this code.

privatevoidsetUpMapIfNeeded() {
        // 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) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.if (mMap != null) {
                setUpMap();
            }
        }
    }

    privatevoidsetUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
        mMap.setMyLocationEnabled(true);


    }

Post a Comment for "Nullpointerexception When Trying To Place Marker On Google Map V2"