我試圖在Google Maps Android API v2中使用自定義圖標標記。我剛剛更改了Google提供的一個示例。我加.icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow))
到RawMapViewDemoActivity的方法setUpMap().
「IBitmapDescriptorFactory未初始化」錯誤
private void setUpMapIfNeeded() {
if (mMap == null) {
mMap = ((MapView) findViewById(R.id.map)).getMap();
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions()
.position(new LatLng(0, 0))
.title("Marker")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow)));
}
但我總是得到一個「IBitmapDescriptorFactory未初始化」。
12-18 15:40:54.356: E/AndroidRuntime(12591): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mapdemo/com.example.mapdemo.RawMapViewDemoActivity}: java.lang.NullPointerException: IBitmapDescriptorFactory is not initialized
12-18 15:40:54.356: E/AndroidRuntime(12591): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-18 15:40:54.356: E/AndroidRuntime(12591): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-18 15:40:54.356: E/AndroidRuntime(12591): at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-18 15:40:54.356: E/AndroidRuntime(12591): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-18 15:40:54.356: E/AndroidRuntime(12591): at android.os.Handler.dispatchMessage(Handler.java:99)
12-18 15:40:54.356: E/AndroidRuntime(12591): at android.os.Looper.loop(Looper.java:137)
12-18 15:40:54.356: E/AndroidRuntime(12591): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-18 15:40:54.356: E/AndroidRuntime(12591): at java.lang.reflect.Method.invokeNative(Native Method)
12-18 15:40:54.356: E/AndroidRuntime(12591): at java.lang.reflect.Method.invoke(Method.java:511)
12-18 15:40:54.356: E/AndroidRuntime(12591): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-18 15:40:54.356: E/AndroidRuntime(12591): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-18 15:40:54.356: E/AndroidRuntime(12591): at dalvik.system.NativeStart.main(Native Method)
12-18 15:40:54.356: E/AndroidRuntime(12591): Caused by: java.lang.NullPointerException: IBitmapDescriptorFactory is not initialized
12-18 15:40:54.356: E/AndroidRuntime(12591): at com.google.android.gms.internal.at.a(Unknown Source)
12-18 15:40:54.356: E/AndroidRuntime(12591): at com.google.android.gms.maps.model.BitmapDescriptorFactory.d(Unknown Source)
12-18 15:40:54.356: E/AndroidRuntime(12591): at com.google.android.gms.maps.model.BitmapDescriptorFactory.fromResource(Unknown Source)
12-18 15:40:54.356: E/AndroidRuntime(12591): at com.example.mapdemo.RawMapViewDemoActivity.setUpMap(RawMapViewDemoActivity.java:67)
12-18 15:40:54.356: E/AndroidRuntime(12591): at com.example.mapdemo.RawMapViewDemoActivity.setUpMapIfNeeded(RawMapViewDemoActivity.java:58)
12-18 15:40:54.356: E/AndroidRuntime(12591): at com.example.mapdemo.RawMapViewDemoActivity.onCreate(RawMapViewDemoActivity.java:43)
12-18 15:40:54.356: E/AndroidRuntime(12591): at android.app.Activity.performCreate(Activity.java:5008)
12-18 15:40:54.356: E/AndroidRuntime(12591): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-18 15:40:54.356: E/AndroidRuntime(12591): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-18 15:40:54.356: E/AndroidRuntime(12591): ... 11 more
此前使用從這個類的任何方法,你必須做到以下幾點,以確保這一類被初始化的一個:
等待使GoogleMap可以從已添加到應用程序的MapFragment或MapView中獲取。您可以通過調用getMap()方法並檢查返回的對象是否爲空來驗證GoogleMap是否可用。
調用MapsInitializer.initialize(Context)。只要不引發com.google.android.gms.common.GooglePlayServicesNotAvailableException GooglePlayServicesNotAvailableException,該類將被正確初始化。
我做的第一個,但仍然得到這個錯誤。有什麼建議麼?
我建議創建一個示例項目,可以重現錯誤並將其源代碼發佈到某處。這感覺就像'fromResource()'中的一個錯誤,但我們需要一個可重複的測試用例。 – CommonsWare
調用'MapInitializer.initialize()'對我來說是訣竅。 – Ash
我面臨同樣的問題..java.lang.NullPointerException:IBitmapDescriptorFactory未初始化 – sanjay