Error android.graphics.Canvas.throwIfRecycled when

2019-06-17 23:57发布

Im trying to overlay images on a canvas using the following method:

private Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {

    bmOverlay = Bitmap.createBitmap(70, 70, Bitmap.Config.RGB_565);
    canvas = new Canvas(bmOverlay);
    canvas.drawBitmap(bmp1, 0, 0, null);  //line 179
    canvas.drawBitmap(bmp2, 0, 0, null);
    return bmOverlay;           
}

However, my app keeps crashing and the log reads:

java.lang.NullPointerException at android.graphics.Canvas.throwIfRecycled(Canvas.java:954) at android.graphics.Canvas.drawBitmap(Canvas.java:980) at com.MyApp.overlay(MyApp.java:179)

Can anyone help?

3条回答
beautiful°
2楼-- · 2019-06-18 00:24

I had also this same stack trace and I tried to find a solution from the thread things etc, But then I found out that I never assigned the bitmap variable in my code.

查看更多
▲ chillily
3楼-- · 2019-06-18 00:33

Although this in an old question, I found this to be the solution for me. http://nowherenearithaca.blogspot.com/2011/06/solved-bizarre-null-pointer-thrown-in.html

where they suggest

Try doing a clean in eclipse. It seems to be caching sometimes and can get confused. That seemed to solve it for this particular case.

查看更多
Explosion°爆炸
4楼-- · 2019-06-18 00:46

I had the same trowIfRecycled exception while trying to draw a bitmap to a canvas. I was trying to draw in a thread I started, before the program got around to initializing the bitmap. So in my case: bitmap was null and I had to look for a better place to do the initializing.

查看更多
登录 后发表回答