SurfaceHolder.lockCanvas() Returning null
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?
![Jesse Jashinsky](https://i.stack.imgur.com/ODdzp.png?s=256&g=1)
Jesse Jashinsky
I'm a general programmer. I can get into just about anything if I want/need to.
Updated on June 09, 2022Comments
-
Jesse Jashinsky about 2 years
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 fromGameActivity
, and the thread then runs everything from itsGameThread.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?