user1610177 user1610177 - 3 months ago 14
Android Question

NullPointerException whilst trying to add polyline/marker to a GoogleMap

I have a SupportMapFragment with a GoogleMap object (which I have manipulated already using a GoogleMapOptions) and it is displaying/functioning fine, until I call getMap().addPolyline(...) where I get a NullPointerException.

Here is my layout xml for the Activity:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:id="@+id/tripSummary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="@string/trip_summary"
tools:context=".Start" />

<TextView
android:id="@+id/tripDetails"
android:layout_below="@id/tripSummary"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="@string/awaiting_location"
tools:context=".Start" />

<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_below="@id/tripDetails"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraZoom="13"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>


Here is the createMap method in my Activity (which extends android.support.v4.app.FragmentActivity) which is called in onCreate(...)

private void createMap(List<LatLng> latLngs) {

float zoom = 13;
PolylineOptions poly = new PolylineOptions()
.color(Color.RED);
MarkerOptions startMarker = new MarkerOptions()
.position(latLngs.get(0))
.title("Start");
MarkerOptions endMarker = new MarkerOptions()
.position(latLngs.get(latLngs.size() - 1))
.title("End");
LatLng startLatLng = latLngs.get(0);
GoogleMapOptions mapOptions = new GoogleMapOptions();
CameraPosition cameraP = new CameraPosition(startLatLng, zoom, 0, 0);

if (mMap == null) {

// instantiate map
mMapFragment = SupportMapFragment.newInstance(mapOptions);
FragmentTransaction fragmentTransaction = getSupportFragmentManager()
.beginTransaction();
fragmentTransaction.add(R.id.map, mMapFragment);
fragmentTransaction.commit();
Log.d("Map", "fragment transaction complete");

mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

// check it has been instantiated
if (mMapFragment != null) {

mapOptions.mapType(GoogleMap.MAP_TYPE_SATELLITE)
.camera(cameraP)
.compassEnabled(true);


//Add LatLngs to a polyline
for(LatLng latLng : latLngs){
poly.add(latLng);
}

mMapFragment.getMap().addPolyline(poly);
mMapFragment.getMap().addMarker(startMarker);
mMapFragment.getMap().addMarker(endMarker);
}
}
}


LogCat:

01-03 20:42:42.737: E/AndroidRuntime(18116): FATAL EXCEPTION: main
01-03 20:42:42.737: E/AndroidRuntime(18116): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project.locationapp/com.project.locationapp.TripSummary}: java.lang.NullPointerException
01-03 20:42:42.737: E/AndroidRuntime(18116): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
01-03 20:42:42.737: E/AndroidRuntime(18116): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
01-03 20:42:42.737: E/AndroidRuntime(18116): at android.app.ActivityThread.access$1500(ActivityThread.java:121)
01-03 20:42:42.737: E/AndroidRuntime(18116): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
01-03 20:42:42.737: E/AndroidRuntime(18116): at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 20:42:42.737: E/AndroidRuntime(18116): at android.os.Looper.loop(Looper.java:130)
01-03 20:42:42.737: E/AndroidRuntime(18116): at android.app.ActivityThread.main(ActivityThread.java:3701)
01-03 20:42:42.737: E/AndroidRuntime(18116): at java.lang.reflect.Method.invokeNative(Native Method)
01-03 20:42:42.737: E/AndroidRuntime(18116): at java.lang.reflect.Method.invoke(Method.java:507)
01-03 20:42:42.737: E/AndroidRuntime(18116): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
01-03 20:42:42.737: E/AndroidRuntime(18116): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
01-03 20:42:42.737: E/AndroidRuntime(18116): at dalvik.system.NativeStart.main(Native Method)
01-03 20:42:42.737: E/AndroidRuntime(18116): Caused by: java.lang.NullPointerException
01-03 20:42:42.737: E/AndroidRuntime(18116): at com.project.locationapp.TripSummary.createMap(TripSummary.java:113)
01-03 20:42:42.737: E/AndroidRuntime(18116): at com.project.locationapp.TripSummary.onCreate(TripSummary.java:38)
01-03 20:42:42.737: E/AndroidRuntime(18116): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-03 20:42:42.737: E/AndroidRuntime(18116): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
01-03 20:42:42.737: E/AndroidRuntime(18116): ... 11 more


I have tested the data source used to get the LatLng values and it retrieves the values fine. Any suggestions as to why I am getting a
NullPointerException
when trying to get the map from the
SupportMapFragment
?

Thanks in advance.

Ed

Answer

If you add the SupportMapFragment to the layout file you don't have to create a new instance manually. You just need to obtain the reference by calling

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
GoogleMap map = mapFragment.getMap();

You are probably getting the NPE because the newly instantiated fragment haven't created the GoogleMap object yet.

Comments