简体   繁体   English

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

[英]Android Drawing Bitmap Causes Null Pointer Exception

I'm trying to draw a bitmap but I get a nullpointerexception...我正在尝试绘制位图,但出现空指针异常...

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

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

It gives this error message:它给出了这个错误信息:

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)

Edited :编辑

As @throwaway pointed out, Paint can be null, and since the exception started inside throwIfCannotDraw, I suppose the null reference was the bitmap, since:正如@throwaway 指出的,Paint 可以为空,并且由于异常在 throwIfCannotDraw 内部开始,我认为空引用是位图,因为:

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);
    }
}

So this might be the same issue as Android: BitmapFactory.decodeResource returning null所以这可能是与Android相同的问题:BitmapFactory.decodeResource 返回 null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM