我有一个GoogleMap对象(我已经使用GoogleMapOptions已经操作了)一个SupportMapFragment,它会显示/运转正常,直到我打电话的GetMap()。addPolyline(...),我得到一个NullPointerException。
下面是我对活动布局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>
这是在我的活动的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);
}
}
}
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
我已经测试了用于获取经纬度值的数据源,并检索值的罚款。 任何建议,为什么我得到一个NullPointerException
试图从该地图时SupportMapFragment
?
提前致谢。
埃德