gpt4 book ai didi

android - lockCanvas() 上的 NullPointerException

转载 作者:太空狗 更新时间:2023-10-29 12:56:39 28 4
gpt4 key购买 nike

我正在为 Android 开发一个小游戏(测试),但我不明白用 SurfaceView 创建一个好的 Timer 线程的逻辑。

[SurfaceView object].getHolder().lockCanvas(null) 出现问题。问题是 lockCanvas 返回无效值(在本例中为 null)。

我无法捕捉到这个异常,因为我……不能! :/

这是我的代码:

package game;

import game.logics.SceneGroup;
import game.scenes.TeamScene;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;

// Definição da Atividade
public class GameActivity extends Activity {
// Definição do Ambiente
public class GameSurface extends SurfaceView
implements SurfaceHolder.Callback {
public GameSurface(final Context context) {
super(context);

this.getHolder().addCallback(this);
this.setFocusable(true);
}

@Override
public void surfaceChanged(final SurfaceHolder arg0,
final int arg1, final int arg2, final int arg3) {}

@Override
public void surfaceCreated(final SurfaceHolder arg0) {
GameActivity.this.running = true;
}

@Override
public void surfaceDestroyed(final SurfaceHolder arg0) {
GameActivity.this.running = false;
}

public void update(final Canvas canvas) {
GameActivity.this.scenes.update(canvas);
}
}

class GameTask extends TimerTask {
@Override
public void run() {
if(GameActivity.this.running == false) {
return;
}

final SurfaceHolder holder =
GameActivity.this.surface.getHolder();

Canvas canvas = null;
try {
canvas = holder.lockCanvas();
canvas.drawColor(Color.BLACK);

synchronized(holder) {
GameActivity.this.surface.update(canvas);
}
}
finally {
if(canvas != null) {
holder.unlockCanvasAndPost(canvas);
}
}
}
}

private GameSurface surface;
private SceneGroup scenes;

private Timer timer;
private TimerTask task;
private boolean running = false;

@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

// ### SOLVE: PROBLEM START HERE ###
this.surface = new GameSurface(this);

this.scenes = new SceneGroup(this);
this.scenes.add(new TeamScene(this));

this.timer = new Timer();
this.task = new GameTask();

// ### SOLVE: PROBLEM END HERE ###
// ### Invalid new instance of GameSurface ###
this.setContentView(new GameSurface(this));
}

@Override
public void onPause() {
super.onPause();
this.timer.cancel();
}

@Override
public void onResume() {
super.onResume();
this.timer.scheduleAtFixedRate(this.task, 0, 33);
}
}

我做得对吗?我可以做些什么来修复我的代码?

最佳答案

您正在创建两次 GameSurface。

来到这里:

this.surface = new GameSurface(this);

这里又是:

this.setContentView(new GameSurface(this));

大概他们应该是同一个吧?

关于android - lockCanvas() 上的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6050323/

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