“IBitmapDescriptorFactory未初始化”错误

时间:2012-12-18 15:03:19

标签: android google-maps-android-api-2

我正在尝试在Google Maps Android API v2中使用自定义图标获取标记。我刚刚更改了Google提供的一个示例。我在方法.icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow))

中将setUpMap().添加到RawMapViewDemoActivity
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

BitmapDescriptorFactory中说:

  

在使用此类中的任何方法之前,必须执行以下操作之一以确保初始化此类:

     
      
  • 等待Google地图从您添加到应用程序的MapFragment或MapView中可用。您可以通过调用getMap()方法并检查返回的对象是否为空来验证GoogleMap是否可用。

  •   
  • 调用MapsInitializer.initialize(Context)。只要不抛出com.google.android.gms.common.GooglePlayServicesNotAvailableException GooglePlayServicesNotAvailableException,就会正确初始化此类。

  •   

我做了第一个,但仍然收到此错误。有什么建议吗?

7 个答案:

答案 0 :(得分:99)

MapsInitializer.initialize(getApplicationContext())

中致电onCreate()

答案 1 :(得分:20)

onCreate()

中调用以下内容
try {
        MapsInitializer.initialize(getApplicationContext());
    } catch (GooglePlayServicesNotAvailableException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

答案 2 :(得分:16)

将您的代码移至'onMapReady()',这是GoogleMaps API提供的回调。

它会起作用!

答案 3 :(得分:1)

这发生在我身上,最后在我的情况下我能找到的是没有安装谷歌播放服务。所以安装它们和应用程序没有崩溃。因此,可以在catch块中设置try和对话框。

答案 4 :(得分:0)

请参见Custom marker in google maps in android with vector asset icon

就我而言,我在Google Maps上有标记。但是在API 19仿真器上,Google Play Services有问题。因此,此错误具有误导性,Google Play Services试图创建BitmapDescriptor,但没有。

您应该在onMapReady()中创建标记,并使用Google Maps将标记移到其他位置。

如果在Android 4上显示Google Maps时遇到问题,也许应该将库从16.1.0降级到16.0.0,请参见https://stackoverflow.com/a/54772374/2914140。但是,如果这样做,您将在Androif 9上崩溃,请参阅Google Maps crashing on Android Pie

答案 5 :(得分:0)

好的,这是一种对我有用的解决方案,我偶然发现了它! 只需将您的Gradle升级到 com.android.tools.build:gradle:3.6.2

    dependencies {
    classpath 'com.android.tools.build:gradle:3.6.2'


    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

答案 6 :(得分:-1)

  GoogleApiAvailability googleApiAvailability=GoogleApiAvailability.getInstance();

  int status=googleApiAvailability.isGooglePlayServicesAvailable(getActivity());



  if (status != ConnectionResult.SUCCESS) {
        int requestCode = 10;
        Dialog dialog = googleApiAvailability.getErrorDialog(getActivity(),status,requestCode);
        dialog.show();
    }else {}