我有一个带有 GoogleMap 对象的 SupportMapFragment(我已经使用 GoogleMapOptions 对其进行了操作)并且它显示/运行良好,直到我调用 getMap().addPolyline(...) 时我得到一个 NullPointerException。
这是我的 Activity 布局 xml:
<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>
这是我的 Activity 中的 createMap 方法(它扩展了 android.support.v4.app.FragmentActivity),它在 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);
}
}
}
日志猫:
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
我已经测试了用于获取 LatLng 值的数据源,它可以很好地检索这些值。关于为什么我NullPointerException
在尝试从 获取地图时得到的任何建议SupportMapFragment
?
提前致谢。
埃德