2

I have a NullPointerException being thrown in my code:

canvas.drawBitmap(icon, null, rect, null);

I have a Log statement right above there to make sure the canvas is not null. I'm guessing the 2nd Rect being null is the problem? I thought it was ok to leave it as null.

Here is the logCat output:

07-22 21:34:16.807: E/AndroidRuntime(19340): FATAL EXCEPTION: Thread-3384
07-22 21:34:16.807: E/AndroidRuntime(19340): java.lang.NullPointerException
07-22 21:34:16.807: E/AndroidRuntime(19340):    at
android.view.GLES20RecordingCanvas.drawBitmap(GLES20RecordingCanvas.java:118)
07-22 21:34:16.807: E/AndroidRuntime(19340):    at   
com.live.LedgeView$DrawLedgeThread.run(LedgeView.java:88)
user1154644
  • 4,491
  • 16
  • 59
  • 102

5 Answers5

1

Yes. Please make sure the "icon" bitmap is not null and "rect" also,

 Bitmap icon= BitmapFactory.decodeResource(getResources(),
                            R.drawable.icon);

Please refer these link. It may help you out. Click Here to see sample 1

Click here to see sample 2

Community
  • 1
  • 1
Srinivasan
  • 4,481
  • 3
  • 28
  • 36
0

From the Android source code:

public void drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) {
    if (dst == null) {
        throw new NullPointerException();
    }
    throwIfRecycled(bitmap);
    native_drawBitmap(mNativeCanvas, bitmap.ni(), src, dst,
                      aint != null ? paint.mNativePaint : 0,
                      mScreenDensity, bitmap.mDensity);
}

So...are you sure dst is not null?

In any case post the logcat stack trace.

Edit: In response to one of your comments, never update UI components from outside the UI Thread. Use AsyncTask if you need to fetch the Bitmap asynchronously.

new AsyncTask(Void,Void,Bitmap){

    protected Bitmap doInBackground(Void... voids){
        //fetch Bitmap
        return bitmap;
    }

    protected void onPostExecute(Bitmap bitmap){
        //fetch Bitmap
        canvas.drawBitmap(bitmap,...);
    }

}
type-a1pha
  • 1,891
  • 13
  • 19
0

usually, I do this way. I decode bitmap by a thread, and put it into a queue. synchronize the queue with UI thread. the UI thread gets bitmap from the queue, and draw it. all UI stuff should be done in UI thread.

yushulx
  • 11,695
  • 8
  • 37
  • 64
0

I had a Null Pointer Ex. in android.view.GLES20RecordingCanvas.drawBitmap

I read that you must do the "drawBitmap" inside of your onDraw method (in your view class).

for me, i used the invalidate() method for trigger onDraw. Before calling invalidate() i set some parameters to my object (of view class), so, when onDraw is called, it uses the parameters I put.

Hope it works :)

Leonardo.

0
public void drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) {
    if (dst == null) {
        throw new NullPointerException();
    }
    throwIfRecycled(bitmap);
    native_drawBitmap(mNativeCanvas, bitmap.ni(), src, dst,
            aint != null ? paint.mNativePaint : 0, mScreenDensity,
            bitmap.mDensity);
}

I have fixed this issue by checking dst for null before sending to that method. If dst is null i have used local variable that i have stored every time on the method "onBoundsChange". This resolve my issue.

Ganesh Kanna
  • 2,269
  • 1
  • 19
  • 29