2

我创建了一个 Android 应用程序,它会在屏幕上绘制一个小图像,如果你点击屏幕底部 50 像素,它就会退出。当我点击那里时,它退出正常,但是,当我按下后退(或主页)按钮退出应用程序时,它退出,然后说,“不幸的是,游戏开发已经停止。” 我还检查了 LogCat,它显示每次停止工作时都会发生 Java NullPointerException。LogCat 的输出是:

10-30 01:13:28.280: E/AndroidRuntime(15294): FATAL EXCEPTION: Thread-1718
10-30 01:13:28.280: E/AndroidRuntime(15294): java.lang.NullPointerException
10-30 01:13:28.280: E/AndroidRuntime(15294):    at tricrose.gamedev.GameView.onDraw(GameView.java:56)
10-30 01:13:28.280: E/AndroidRuntime(15294):    at tricrose.gamedev.GameThread.run(GameThread.java:30)

我有三个 Java 类:Game.java(Activity)、GameView.java(SurfaceView)和 GameThread.java(主线程)。

代码如下:

游戏.java:

package tricrose.gamedev;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;

public class Game extends Activity {

    public static final String TAG = Game.class.getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(new GameView(this));
        Log.d(TAG, "View added");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_game, menu);
        return true;
    }
}

游戏视图.java:

package tricrose.gamedev;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GameView extends SurfaceView implements SurfaceHolder.Callback {

    public static final String TAG = GameView.class.getSimpleName();
    private GameThread thread;

    public GameView(Context context) {
        super(context);
        getHolder().addCallback(this);
        thread = new GameThread(getHolder(), this);
        setFocusable(true);
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    public void surfaceCreated(SurfaceHolder holder) {
        thread.setRunning(true);
        thread.start();
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        boolean retry = true;
        while (retry) {
            try {
                thread.join();
                retry = false;
            } catch (Exception e) {}
        }
    }

    public boolean onTouchEvent(MotionEvent e) {
        if (e.getAction() == MotionEvent.ACTION_DOWN) {
            if (e.getY() > getHeight() - 60) {
                thread.setRunning(false);
                ((Activity)getContext()).finish();
            }
        }
        return super.onTouchEvent(e);
    }

    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.droid_1), 10, 10, null);
    }
}

游戏线程.java:

package tricrose.gamedev;

import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class GameThread extends Thread {

    public static final String TAG = GameThread.class.getSimpleName();
    private boolean running;
    private SurfaceHolder surfaceHolder;
    private GameView gameView;

    public GameThread(SurfaceHolder surfaceHolder, GameView gameView) {
        super();
        this.surfaceHolder = surfaceHolder;
        this.gameView = gameView;
    }

    public void setRunning(boolean running) {
        this.running = running;
    }

    public void run() {
        Canvas canvas;
        while (running) {
            canvas = null;
            try {
                canvas = this.surfaceHolder.lockCanvas();
                synchronized (surfaceHolder) {
                    this.gameView.onDraw(canvas);
                }
            } finally {
                if (canvas != null) {
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
        }
    }
}

我已经在互联网上搜索了一个解决方案,但我找不到一个真正有效的解决方案,因此非常感谢任何帮助。

4

3 回答 3

2

我终于设法找到了解决方案!事实证明,我应该在调用 onDraw 之前检查画布是否为空,如果为空,则通过完成活动退出应用程序。我在 JavaCodeGeeks.com 上找到了解决方案,当时我正在查看 Android 教程。感谢大家的所有回答!

于 2012-10-31T00:03:32.477 回答
0

为什么要从 onDraw 和 GameThread 运行方法的两个地方进行绘图。我认为你只需要从 GameThread rlun 方法中删除 onDraw 做每一个绘图。

于 2012-10-30T12:29:44.217 回答
0

这只是一个猜测,但当应用程序尝试退出时,您的应用程序可能仍在绘图。在您的 onDraw 方法中,您调用这行代码(这可能是您的问题原因):
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.droid_1), 10, 10, null);
在这种情况下,您可能不再有可用的资源;)

您可以尝试:
1. 将一个类私有位图变量放入您的 GameView 类:
private Bitmap mBitmap;
2. 在 GameView 构造函数中加载位图:
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.droid_1);
3. 使用(已加载的)位图进行绘制:
canvas.drawBitmap(mBitmap, 10, 10, null);

希望有帮助!

于 2012-10-30T12:33:02.613 回答