Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 848
Jesse Jashinsky Avatar asked Aug 03 '11 03:08

Jesse Jashinsky


1 Answers

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?

like image 109
Lumis Avatar answered Oct 06 '22 04:10

Lumis