android截图canvas为空,Android canvas.drawColor抛出一个空的对象引用

我有两个几乎完全相同的方法,第一个是在资源加载时绘制启animation面,第二个是将所有资源绘制到屏幕上。 第一个在这一行中抛出一个NullPointerException:

canvas.drawColor(Color.argb(255, 0, 0, 0));

这是我的错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.Canvas.drawColor(int)' on a null object reference

10-19 21:54:56.042 18973-18973/? E/AndroidRuntime: at com.xyz.123.GameView.drawSplash(GameView.java:76)

这里是我的代码完整:package com.xyz.123; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; public class GameView extends SurfaceView implements Runnable { volatile boolean playing; Thread gameThread = null; private Paint paint; private Canvas canvas; private SurfaceHolder holder; private int screenWidth; private int screenHeight; private Background1ResourceList background1ResourceList; private Background background1; private Background background2; private Background background3; private Context context; private SplashScreen splashScreen; public GameView(Context context, int screenWidth, int screenHeight) { super(context); this.context = context; this.screenWidth = screenWidth; this.screenHeight = screenHeight; // Initialize our drawing objects holder = getHolder(); paint = new Paint(); splashScreen = new SplashScreen(context, screenWidth, screenHeight); drawSplash(); background1ResourceList = new Background1ResourceList(); background1 = new Background(context, background1ResourceList.getBackgroundResources(), screenWidth, screenHeight, 0.00f, 0.50f); background2 = new Background(context, background1ResourceList.getBackgroundResources(), screenWidth, screenHeight, 0.33f, 0.80f); background3 = new Background(context, background1ResourceList.getBackgroundResources(), screenWidth, screenHeight, 0.66f, 1.00f); } @Override public void run() { while (playing) { update(); draw(); control(); } } private void update() { background1.update(); background2.update(); background3.update(); } private synchronized void drawSplash() { if (holder.getSurface().isValid()) { //First we lock the area of memory we will be drawing to holder.getSurface(); canvas = holder.lockCanvas(); // rub out the last frame canvas.drawColor(Color.argb(255, 0, 0, 0)); // draw the backgrounds splashScreen.draw(canvas, paint); // unlock and draw the scene holder.unlockCanvasAndPost(canvas); } else { System.out.println("surface not valid"); } } private synchronized void draw() { if (holder.getSurface().isValid()) { //First we lock the area of memory we will be drawing to canvas = holder.lockCanvas(); // rub out the last frame canvas.drawColor(Color.argb(255, 0, 0, 0)); // draw the backgrounds background1.draw(canvas, paint); background2.draw(canvas, paint); background3.draw(canvas, paint); holder.unlockCanvasAndPost(canvas); } } public void pause() { playing = false; try { gameThread.join(); } catch (InterruptedException e) { } } public void resume() { playing = true; gameThread = new Thread(this); gameThread.start(); } }

canvas为空的原因是因为我试图从GameView(这是一个表面视图)的构造函数中获取canvas。

我将调用drawSplash方法移到游戏循环之前的run方法中,并且完美地工作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值