本文介绍了SurfaceHolder.lockCanvas()返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个游戏,因为我是新来的Andr​​oid,基于我设计过的例子中的。在它的设计, GameThread.doStart() GameActivity 调用,以及线程,然后运行一切从如下图所示GameThread.run()循环(大部分的code已被删除的清晰度):

I'm making a game, and since I'm new to Android, I based the design off of the example LunarLander code. In its design, GameThread.doStart() is called from GameActivity, and the thread then runs everything from its GameThread.run() loop as shown below (much of the code has been removed for clarity):

public class GameActivity extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        gameView = new GameView(this);
        fl = new FrameLayout(this);
        fl.addView(gameView);

        setContentView(fl);

        gameThread = gameView.getThread();
        gameThread.doStart();
    }
}

class GameThread extends Thread
{
    public void doStart()
    {
    }

    public void run()
    {
        while (running)
        {
            Canvas c = null;
            try
            {
                c = mSurfaceHolder.lockCanvas();
                // Use canvas
            }
            finally
            {
            }
        }
    }
}

要尝试解决一些问题,我已经运行到我试图把更多的控制GameActivity手中,如下图所示:

To try to fix some problems I've been running into I tried to put more control in the hands of GameActivity, as shown below:

public class GameActivity extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        gameView = new GameView(this);
        fl = new FrameLayout(this);
        fl.addView(gameView);

        setContentView(fl);

        gameThread = gameView.getThread();

        while (gameThread.isRunning())
        {
            gameThread.run();
        }
    }
}

class GameThread extends Thread
{
    public void doStart()
    {
    }

    public void run()
    {
        Canvas c = null;
        try
        {
            c = mSurfaceHolder.lockCanvas();
            // Use canvas
        }
        finally
        {
        }
    }
}

除了当我这样做 lockCanvas()始终返回null,因此我不能画任何东西到屏幕上。正如我所说的,我还是新的Andr​​oid,所以我不知道为什么第二种情况下是行不通的。任何人都知道发生了什么,或者为什么它不工作?

Except when I do that lockCanvas() always returns null, and thus I can't draw anything to the screen. As I said I'm still new to Android so I have no idea why the second case isn't working. Anyone know what's going on or why it's not working?

修改:从我测试过,表面上是从未创建。在 GameView.onResume()我在移动与游戏之前创建的活动等待表面,但它永远等待。为什么第一个实例是创建表面的唯一情况?

EDIT: From what I've tested, the surface is never created. During GameView.onResume() I had the activity wait for the surface to be created before moving on with the game, but it waited forever. How come the first instance is the only case where the surface is created?

推荐答案

您正在开始一个thred在创建活动中,同时被创建并准备了SurfaceView时,应只启动。检查此第二,在SurfaceView例如:how我可以用动画框架画布里面?

You are starting a thred in activity on create, while it should be started only when the SurfaceView is created and ready. Check this the second, the SurfaceView example: how can I use the animation framework inside the canvas?

这篇关于SurfaceHolder.lockCanvas()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 21:15