我刚刚完成了一个小游戏,第二个,基于java4edu完成的代码: http://www.edu4java.com/en/game/game0-en.html
然而,在我的mod之后,我遇到了一个小故障,无论什么时候我,恢复,旋转屏幕,将屏幕更改为(从图库加载),或者将画布发送到背景的任何内容; onresume,我得到一个错误,其中logcat总是指向这个: canvas.drawBitmap(bka,0,0,null);我认为更重要的是我需要在合适的时间停止我的游戏循环线程,以便刷新并重新加载绘制值,但是idk,建议请给我一个!!
package com.doggy.__elite;
import android.annotation.SuppressLint;
import android.graphics.Canvas;
public class GameLoopThread extends Thread {
static final long FPS = 100;
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);
}
}
}
}
}
第141行是: bka = Bitmap.createScaledBitmap(bka,Dwidtha,Dheighta,true); - 要么 - canvas.drawBitmap(bka,0,0,null); 我发现暂停问题,我只需要添加: public void onPause(Bundle savedInstanceState){ super.onPause(); } :)
恢复时会发生这种情况:
11-24 12:16:12.538:E / AndroidRuntime(20772):致命异常:主要 11-24 12:16:12.538:E / AndroidRuntime(20772):java.lang.NullPointerException 11-24 12:16:12.538:E / AndroidRuntime(20772):at com.doggy .__ elite.GameView $ 1.surfaceCreated(GameView.java:66) 11-24 12:16:12.538:E / AndroidRuntime(20772):在android.view.SurfaceView.updateWindow(SurfaceView.java:545) 11-24 12:16:12.538:E / AndroidRuntime(20772):在android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:226) 11-24 12:16:12.538:E / AndroidRuntime(20772):在android.view.View.dispatchWindowVisibilityChanged(View.java:5851) 11-24 12:16:12.538:E / AndroidRuntime(20772):在android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:951) 11-24 12:16:12.538:E / AndroidRuntime(20772):在android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:951) 11-24 12:16:12.538:E / AndroidRuntime(20772):在android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:951) 11-24 12:16:12.538:E / AndroidRuntime(20772):在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1038) 11-24 12:16:12.538:E / AndroidRuntime(20772):在android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2583) 11-24 12:16:12.538:E / AndroidRuntime(20772):在android.os.Handler.dispatchMessage(Handler.java:99) 11-24 12:16:12.538:E / AndroidRuntime(20772):在android.os.Looper.loop(Looper.java:137) 11-24 12:16:12.538:E / AndroidRuntime(20772):在android.app.ActivityThread.main(ActivityThread.java:4508) 11-24 12:16:12.538:E / AndroidRuntime(20772):at java.lang.reflect.Method.invokeNative(Native Method) 11-24 12:16:12.538:E / AndroidRuntime(20772):at java.lang.reflect.Method.invoke(Method.java:511) 11-24 12:16:12.538:E / AndroidRuntime(20772):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:809) 11-24 12:16:12.538:E / AndroidRuntime(20772):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576) 11-24 12:16:12.538:E / AndroidRuntime(20772):at dalvik.system.NativeStart.main(Native Method)
第66行是: gameLoopThread.setRunning(真);