按下后退按钮后Android应用程序无法正常工作

时间:2012-10-30 12:14:19

标签: java android eclipse sdk

我创建了一个Android应用程序,它可以在屏幕上绘制一个小图像并退出,如果你点击屏幕的底部50px。当我点击它时,它退出正常,但是,当我按下后面(或主页)按钮退出应用程序时,它退出,但随后说,“不幸的是,游戏开发已停止。”我还检查了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(主线程)。

代码如下:

Game.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;
    }
}

GameView.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);
    }
}

GameThread.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);
                }
            }
        }
    }
}

我在互联网上搜索了一个解决方案,我找不到一个真正有用的解决方案,所以非常感谢任何帮助。

3 个答案:

答案 0 :(得分:2)

我终于找到了解决方案!事实证明,在调用onDraw之前我应该​​检查画布是否为null,如果它为null,则通过完成活动退出应用程序。我在JavaCodeGeeks.com上找到了解决方案,在那里我看了一个Android教程。谢谢大家的所有答案!

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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

希望有所帮助!