gpt4 book ai didi

java - 按下后退按钮后 Android 应用程序无法运行

转载 作者:行者123 更新时间:2023-11-30 09:30:45 25 4
gpt4 key购买 nike

我创建了一个 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;
}
}

游戏 View .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);
}
}
}
}
}

我在整个互联网上搜索了一种解决方案,但找不到实际有效的解决方案,因此非常感谢您的帮助。

最佳答案

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

关于java - 按下后退按钮后 Android 应用程序无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13138758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com