Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
489 views
in Technique[技术] by (71.8m points)

android - SurfaceHolder.lockCanvas() Returning null

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...