【发布时间】:2018-10-14 08:39:00
【问题描述】:
在我的地图中,我尝试显示高质量标记,因此我使用 svg 文件中的 xml 标记而不是使用低 png 图片,因此我将 xml 转换为位图:
public BitmapDescriptor vectorToBitmap(Context context, @DrawableRes int id)
{
Drawable vectorDrawable = ContextCompat.getDrawable(context, id);
int h = Objects.requireNonNull(vectorDrawable).getIntrinsicHeight();
int w = Objects.requireNonNull(vectorDrawable).getIntrinsicWidth();
vectorDrawable.setBounds(0, 0, w, h);
Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bm);
vectorDrawable.draw(canvas);
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(bm);
return bitmapDescriptor;
}
然后我通过以下方式调用此方法:
BitmapDescriptor marker = vectorToBitmap(getActivity(),R.drawable.pin_work);
在许多设备上一切正常,但有时我会收到此错误
Context.getDrawable(int) on a null object reference
如何使用 LG 智能手机专门解决此问题?
谢谢
【问题讨论】:
标签: android nullpointerexception maps marker