在屏幕上绘制位图时出现错误

时间:2019-04-16 19:58:17

标签: java android bitmap bitmapfactory

我是android的新手,但是我有这个,我的应用在部署时崩溃了

public class DungeonExplorer extends View {

    private int CHIBI_SIZE = 125;

    Bitmap chibiSprite;
    Rect sourceRect;
    Rect destinationRect;
    Paint emptyPaint = new Paint();

public DungeonExplorer(Context context) {
        super(context);
        init();
}

private void init(){
        chibiSprite = BitmapFactory.decodeResource(getResources(), R.drawable.chibi_sprite);
        sourceRect = new Rect(0, 2* CHIBI_SIZE, CHIBI_SIZE, 3* CHIBI_SIZE);
        destinationRect = new Rect(100, 100, 500, 500);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawCharacter(canvas);
    }

    private void drawCharacter(Canvas canvas){
        canvas.drawBitmap(chibiSprite, sourceRect, destinationRect, emptyPaint);
    }

问题出在方法drawCharacter / onDraw上,我不确定我在做什么错

  

正在运行的错误消息:java.lang.NullPointerException

1 个答案:

答案 0 :(得分:0)

The View class has many constructors. Be sure you override all constructors and use init() in all of them

相关问题