SurfaceHolder.lockCanvas() Returning null

半世苍凉 提交于 2019-12-29 00:10:23

问题


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
            {
            }
        }
    }
}

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
        {
        }
    }
}

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?

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?


回答1:


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?




回答2:


Start running the thread once the surface gets created. i.e. when you get a callback surfaceCreated(), start the thread.

Code Snippet

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

Shash



来源:https://stackoverflow.com/questions/6921412/surfaceholder-lockcanvas-returning-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!