Android绘图位图导致空指针异常

时间:2014-10-05 18:45:17

标签: java android bitmap nullpointerexception

我正在尝试绘制位图,但我得到了一个nullpointerexception ...

    crab1 = BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_launcher);

    // In the draw method
    canvas.drawBitmap(crab1, screenW / 2, screenH - 2 * rowHeight, null);

它会显示以下错误消息:

java.lang.NullPointerException
at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1083)
at android.graphics.Canvas.drawBitmap(Canvas.java:1139)
at com.coderogden.crabber.TitleView.draw(TitleView.java:100)
at android.view.View.draw(View.java:14501)
at android.view.ViewGroup.drawChild(ViewGroup.java:3102)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2939)
at android.view.View.draw(View.java:14619)
at android.view.View.draw(View.java:14501)
at android.view.ViewGroup.drawChild(ViewGroup.java:3102)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2939)

1 个答案:

答案 0 :(得分:0)

<强>被修改

正如@throwaway指出的那样,Paint可以为null,并且由于异常在throwIfCannotDraw内部启动,我认为null引用是位图,因为:

protected static void throwIfCannotDraw(Bitmap bitmap) {
    if (bitmap.isRecycled()) {
        throw new RuntimeException("Canvas: trying to use a recycled bitmap " + bitmap);
    }
    if (!bitmap.isPremultiplied() && bitmap.getConfig() == Bitmap.Config.ARGB_8888 &&
            bitmap.hasAlpha()) {
        throw new RuntimeException("Canvas: trying to use a non-premultiplied bitmap "
                + bitmap);
    }
}

因此,这可能与Android: BitmapFactory.decodeResource returning null

相同