Issue
I am trying to create a MapView programmatically and add a MarkerPosition as shown below:
MapView mapView = new MapView(getActivity());
((ViewGroup) rootView).addView(mapView);
GoogleMap googleMap = mapView.getMap();
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(
Double.parseDouble(
mEvent.getEventInfo().mEventData.mLat),
Double.parseDouble(
mEvent.getEventInfo().mEventData.mLng)))
.title("Marker"));
Manifest File:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<permission
android:name="com.example.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.permission.MAPS_RECEIVE" />
I am getting the following error:
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference
Solution
because your googleMap is not loaded yet means null and you are adding marker on it which is causing a crash. better to use mapView.getMapAsync(context);
rather than mapView.getMap();
and implement this method
@Override
public void onMapReady(final GoogleMap map) {
if(map != null){
// you are ready to add marker here
}
}
Answered By - Junaid Hafeez
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.