Reputation: 15
I am trying to draw an image onto a canvas, and while the program compiles successfully and behaves as I expect it to, the error log shows that there is a NullPointerException associated with the drawBitmap() method below. What is strange is that my image is still drawn to the canvas. What exactly is the issue and how should I go about resolving it?
My code:
public class ProgressBar extends View
{
String packageName;
public ProgressBar(Context context)
{
super(context);
packageName = context.getPackageName();
}
public ProgressBar(Context context, AttributeSet attribs)
{
super(context, attribs);
packageName = context.getPackageName();
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
int resourceId = getResources().getIdentifier("bar1", "drawable", packageName);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resourceId);
canvas.drawBitmap(bitmap, 35, 35, null);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mWidth = View.MeasureSpec.getSize(widthMeasureSpec);
mHeight = View.MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(mWidth, mHeight);
}
}
Error log:
java.lang.NullPointerException
at android.graphics.Canvas.throwIfRecycled(Canvas.java:1057)
at android.graphics.Canvas.drawBitmap(Canvas.java:1097)
at com.myapp.ProgressBar.onDraw(ProgressBar.java:50)
at android.view.View.draw(View.java:13944)
at android.view.View.draw(View.java:13825)
at android.view.ViewGroup.drawChild(ViewGroup.java:3083)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2920)
at android.view.View.draw(View.java:13823)
at android.view.ViewGroup.drawChild(ViewGroup.java:3083)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2920)
at android.view.View.draw(View.java:13823)
at android.view.ViewGroup.drawChild(ViewGroup.java:3083)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2920)
at android.view.View.draw(View.java:13823)
at android.view.ViewGroup.drawChild(ViewGroup.java:3083)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2920)
at android.view.View.draw(View.java:13947)
at android.view.View.draw(View.java:13825)
at android.view.ViewGroup.drawChild(ViewGroup.java:3083)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2920)
at android.view.View.draw(View.java:13947)
Upvotes: 0
Views: 1060
Reputation: 523
You should never pull a bitmap out of resources in onDraw because onDraw is called numerous times per second, so there is no way it will have time to decode that resource every single time onDraw is called.
You should decode your bitmap in your constructor, save it off in a class variable and use in in onDraw.
So, basically, all you need to do is this:
//add bitmap to class variable
private Bitmap bitmap;
//move these to constructor
int resourceId = getResources().getIdentifier("bar1", "drawable", packageName);
bitmap = BitmapFactory.decodeResource(getResources(), resourceId);
And everything should work.
Upvotes: 1