現在、私はいくつかのウェブサイトのアプリの開発に取り組んでいますが、ゲームアプリの開発に少し夢中になっているので、チュートリアルに従って、最初のレベルでキャンバスに画像を作成し、次のレベルで画像を移動する必要がありますこれもコーディングが行われますが、アプリケーションを実行すると強制的に閉じられます。ここに私のコードがあります。
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)