Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在画布上绘制位图时遇到问题_Android_Canvas_Bitmap - Fatal编程技术网

Android 在画布上绘制位图时遇到问题

Android 在画布上绘制位图时遇到问题,android,canvas,bitmap,Android,Canvas,Bitmap,我正在尝试在画布上绘制图像,当程序成功编译并按预期运行时,错误日志显示存在与下面的drawBitmap()方法相关联的NullPointerException。奇怪的是,我的图像仍然被画在画布上。究竟是什么问题?我应该如何着手解决 我的代码: public class ProgressBar extends View { String packageName; public ProgressBar(Context context) { super(con

我正在尝试在画布上绘制图像,当程序成功编译并按预期运行时,错误日志显示存在与下面的drawBitmap()方法相关联的NullPointerException。奇怪的是,我的图像仍然被画在画布上。究竟是什么问题?我应该如何着手解决

我的代码:

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);
    }
}
错误日志:

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)

您永远不应该从onDraw中的资源中提取位图,因为onDraw每秒被调用多次,所以它不可能在每次调用onDraw时都有时间解码该资源

您应该在构造函数中解码位图,将其保存在类变量中,并在onDraw中使用

所以,基本上,你需要做的就是:

//add bitmap to class variable
private Bitmap bitmap;

//move these to constructor
int resourceId = getResources().getIdentifier("bar1", "drawable", packageName);
bitmap = BitmapFactory.decodeResource(getResources(), resourceId);

一切都应该正常。

我实现了你的建议,但NullPointerException仍然存在。我将在这里进行一个大胆的猜测:这个错误是否可能与Eclipse的图形布局在渲染所有内容时遇到一些问题有关?