在Android中调用createScaledBitmap()方法时出现致命错误

时间:2018-03-21 20:39:22

标签: android bitmap android-bitmap bitmapfactory

我试图在画布上画一幅画,但是当我打电话时:

Bitmap android_guy = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher), 50, 50, true);`

我收到以下错误:

  

引起:java.lang.NullPointerException:     尝试在空对象引用上调用虚方法'int android.graphics.Bitmap.getWidth()'     android.graphics.Bitmap.createScaledBitmap(Bitmap.java:702)`

我没有调用此方法int android.graphics.Bitmap.getWidth()

有人可以解释为什么它会给我这个错误或建议我用另一种方法将图像转换为位图吗?

1 个答案:

答案 0 :(得分:0)

尝试这样

Bitmap.createScaledBitmap(BitmapFactory.decodeResource(context.getResources(),getResources().getDrawable(R.mipmap.ic_launcher),50,50, true);

如果getResources().getDrawable(R.mipmap.ic_launcher)生成错误,请尝试context.getResources().getDrawable(R.mipmap.ic_launcher)