目前我正在为一些网站开发应用程序,但我在开发游戏应用程序时有点疯狂,因此按照教程在第一级Canvas上创建了一个图像,并在下一级别创建了图像为了这个也必须移动编码,但是当我运行应用程序时,它会被强制关闭。 这是我的代码。
public class GameView extends SurfaceView {
private Bitmap bmp;
private SurfaceHolder holder;
private GameLoopThread gameLoopThread;
private int x = 0;
public GameView(Context context) {
super(context);
gameLoopThread = new GameLoopThread()
holder = getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
gameLoopThread.setRunning(false);
while (retry) {
try {
gameLoopThread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
gameLoopThread.setRunning(true);
gameLoopThread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}
});
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
if (x < getWidth() - bmp.getWidth()) {
x++;
}
canvas.drawBitmap( bmp, x, 10, null);
}
线程类
public class GameLoopThread extends Thread{
private GameView view;
private boolean running = false;
public GameLoopThread(GameView view) {
this.view = view;
}
public void setRunning(boolean run) {
running = run;
}
@SuppressLint("WrongCall")
@Override
public void run() {
while (running) {
Canvas c = null;
try {
c = view.getHolder().lockCanvas();
synchronized (view.getHolder()) {
view.onDraw(c);
}
} finally {
if (c != null) {
view.getHolder().unlockCanvasAndPost(c);
}
}
}
}
logcat结果
03-21 23:38:26.341: E/AndroidRuntime(754): FATAL EXCEPTION: main
03-21 23:38:26.341: E/AndroidRuntime(754): java.lang.NullPointerException
03-21 23:38:26.341: E/AndroidRuntime(754): at com.killthemall.GameView$1.surfaceCreated(GameView.java:40)
03-21 23:38:26.341: E/AndroidRuntime(754): at android.view.SurfaceView.updateWindow(SurfaceView.java:543)
03-21 23:38:26.341: E/AndroidRuntime(754): at android.view.SurfaceView.dispatchDraw(SurfaceView.java:348)
03-21 23:38:26.341: E/AndroidRuntime(754): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
03-21 23:38:26.341: E/AndroidRuntime(754): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
03-21 23:38:26.341: E/AndroidRuntime(754): at android.view.View.draw(View.java:6883)
03-21 23:38:26.341: E/AndroidRuntime(754): at android.widget.FrameLayout.draw(FrameLayout.java:357)
03-21 23:38:26.341: E/AndroidRuntime(754): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
03-21 23:38:26.341: E/AndroidRuntime(754): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
03-21 23:38:26.341: E/AndroidRuntime(754): at android.view.View.draw(View.java:6883)
03-21 23:38:26.341: E/AndroidRuntime(754): at android.widget.FrameLayout.draw(FrameLayout.java:357)
03-21 23:38:26.341: E/AndroidRuntime(754): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
03-21 23:38:26.341: E/AndroidRuntime(754): at android.view.ViewRoot.draw(ViewRoot.java:1522)
03-21 23:38:26.341: E/AndroidRuntime(754): at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
03-21 23:38:26.341: E/AndroidRuntime(754): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
03-21 23:38:26.341: E/AndroidRuntime(754): at android.os.Handler.dispatchMessage(Handler.java:99)
03-21 23:38:26.341: E/AndroidRuntime(754): at android.os.Looper.loop(Looper.java:123)
03-21 23:38:26.341: E/AndroidRuntime(754): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-21 23:38:26.341: E/AndroidRuntime(754): at java.lang.reflect.Method.invokeNative(Native Method)
03-21 23:38:26.341: E/AndroidRuntime(754): at java.lang.reflect.Method.invoke(Method.java:507)
03-21 23:38:26.341: E/AndroidRuntime(754): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-21 23:38:26.341: E/AndroidRuntime(754): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-21 23:38:26.341: E/AndroidRuntime(754): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
com.killthemall.GameView $ 1.surfaceCreated(GameView.java:40)
这告诉你在第40行的surfaceCreated
方法中发生了一个空指针。查看该方法,第一行执行gameLoopThread.setRunning
,因此这可能会失败,并带有空指针{{1是空的。
查看代码,您没有初始化exception if gameLoopThread
,因此它的默认值为null。在gameLoopThread
的构造函数中添加行GameView
。