我有一個GoogleMap對象(我已經操縱已經使用GoogleMapOptions)的SupportMapFragment,它顯示/運行良好,直到我調用getMap()。addPolyline(。 ..)我得到一個NullPointerException。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
我已經測試用於獲取緯度數據源Lng值,它檢索的值很好。任何關於爲什麼當我試圖從SupportMapFragment
獲得地圖時獲得NullPointerException
的建議?
在此先感謝。
埃德
據我所知,現在,它正在努力,謝謝!不過,我希望將GoogleMapOptions與Map結合使用,以便在運行時(以及其他位)操作攝像機的位置,並且GoogleMapOptions對象只能添加到創建時的Map中。你知道我可以如何包含GoogleMapOptions對象以用於我的地圖嗎? – user1610177
您可以隨時使用appropiates api在運行時操作地圖選項。包括相機更新。看看api,https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap –
感謝您的時間。 – user1610177