【问题标题】:"IBitmapDescriptorFactory is not initialized" error“IBitmapDescriptorFactory 未初始化”错误
【发布时间】:2012-12-05 19:43:25
【问题描述】:

我正在尝试在 Google Maps Android API v2 中获取带有自定义图标的标记。我刚刚更改了 Google 提供的示例之一。我在方法setUpMap(). 中将.icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow)) 添加到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 中写道:

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

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

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

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

【问题讨论】:

  • 我建议创建一个可以重现错误并将其源代码发布到某处的示例项目。这感觉就像fromResource() 中的一个错误,但我们需要一个可重现的测试用例。
  • 调用 MapInitializer.initialize() 对我有用。
  • 我面临同样的问题..java.lang.NullPointerException: IBitmapDescriptorFactory 未初始化
  • 我的示例 java 类:-pastebin.com/Mh7R0WQV
  • 如果我使用 defaultMarker() 方法而不是 fromResource(),则会发生相同的错误“IBitmapDescriptorFactory 未初始化”。 .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)

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


【解决方案1】:

onCreate()中拨打MapsInitializer.initialize(getApplicationContext())

【讨论】:

  • 这是一个很好的答案,但是首先找出为什么会发生这种情况会很棒,地图教程 AFAIK 中没有记录
  • 即使我添加了这行代码,我的应用程序仍然在某些手机上崩溃,但出现此异常。我发现它可能与未安装或未更新的 GooglePlay 商店有关。有什么方法可以查明地图是否可用?
  • 我正在尝试这个但同样的错误,错误没有任何变化
  • 正确答案来自@Charlie,您应该将代码移动到“onMapReady()”处调用。
  • 尽管这是一个解决方案,但它并不是一个好的解决方案,因为它不适用于所有安卓设备。最好的解决方案是在地图加载并准备好后调用/使用此类。加载并准备好地图后,将通过onMapReady() 方法接收回调,正如@Charlie 在下面的答案中提到的那样。
【解决方案2】:

onCreate()中呼叫关注

try {
        MapsInitializer.initialize(getApplicationContext());
    } catch (GooglePlayServicesNotAvailableException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

【讨论】:

  • 我收到一条消息,这个 try catch 块中永远不会抛出异常?
【解决方案3】:

移动您的代码以调用 onMapReady(),这是由 GoogleMaps API 提供的回调。

【讨论】:

  • 当我放入 onCreate 并在 s5(和模拟器)中失败时,它在 Galaxy s3 上工作(当我在 onCreate 中触发 MapsInitializer 时)。移到 onMapReady 有帮助,干杯!
  • 这其实是正确的答案。其他解决方案并非适用于所有手机。
【解决方案4】:

这发生在我身上,最后在我的情况下,我发现没有安装谷歌播放服务。所以安装它们并且应用程序没有崩溃。所以可能有人可以在 catch 块中放入 try 和一个这样说的对话框。

【讨论】:

    【解决方案5】:

    Custom marker in google maps in android with vector asset icon

    就我而言,我在 Google 地图上有标记。但在 API 19 模拟器上,Google Play Services 存在问题。所以这个错误有误导性,Google Play Services 试图创建BitmapDescriptor,但是不能。

    您应该在onMapReady() 中创建标记,并将任何其他代码移到那里,使用Google Maps

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

    【讨论】:

      【解决方案6】:

      好的,这是一个对我有用的解决方案,我偶然发现了它! 只需将您的 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
      }
      

      【讨论】:

        【解决方案7】:
          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 {}
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-02-05
          • 2021-09-25
          • 1970-01-01
          • 2016-09-20
          • 2019-05-07
          • 2020-05-25
          • 1970-01-01
          相关资源
          最近更新 更多