我在使用 Google 地图 MarkerOptions
创建图标时遇到问题,具体是关于 BitmapDescriptor
和 VectorDrawable
在 API 5.0+ 上的使用。
创建方法如下:
@NonNull
private BitmapDescriptor getBitmapDescriptor(int id) {
return BitmapDescriptorFactory.fromResource(id);
}
如果我尝试使用在xml中定义的VectorDrawable
作为id
参数,一切都很顺利,但是如果googleMap.addMarker(marker)
时BitmapDescriptor
不为空,应用程序总是会崩溃。
11-05 10:15:05.213 14536-14536/xxx.xxxxx.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: xxx.xxxxx.app, PID: 14536
java.lang.NullPointerException
at com.google.a.a.ae.a(Unknown Source)
at com.google.maps.api.android.lib6.d.dn.<init>(Unknown Source)
at com.google.maps.api.android.lib6.d.dm.a(Unknown Source)
at com.google.maps.api.android.lib6.d.ag.<init>(Unknown Source)
at com.google.maps.api.android.lib6.d.eu.a(Unknown Source)
at com.google.android.gms.maps.internal.j.onTransact(SourceFile:167)
at android.os.Binder.transact(Binder.java:380)
at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.addMarker(Unknown Source)
at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
at xxx.xxxxx.app.ui.details.DetailActivity.lambda$initGoogleMaps$23(DetailActivity.java:387)
at xxx.xxxxx.app.ui.details.DetailActivity.access$lambda$10(DetailActivity.java)
at xxx.xxxxx.app.ui.details.DetailActivity$$Lambda$13.onMapReady(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source)
at com.google.android.gms.maps.internal.zzl$zza.onTransact(Unknown Source)
at android.os.Binder.transact(Binder.java:380)
at com.google.android.gms.maps.internal.av.a(SourceFile:82)
at com.google.maps.api.android.lib6.d.fa.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
无论我如何检索drawable,尝试使用BitmapFactory.fromResources
创建位图,然后使用BitmapDescritpionFactory.fromBitmap
,但结果都是一样的。它只是无法处理矢量图形。也尝试了不同的向量,但似乎这里的问题不在于向量的复杂性。有人知道如何修复这个崩溃吗?
@edit
看起来问题不在于
BitmapDescriptior
本身,而在于加载VectorDrawable
时返回了不正确的位图。但是答案中提出的解决方案仍然是好的。