我有两个几乎完全相同的方法,第一个是在资源加载时绘制启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方法中,并且完美地工作。