I'm developing an android game, and my screen is not displaying what I want it to. I've read various tutorials but they are either outdated or hard to follow. I have followed this tutorial and most of the code is identical. However, when running the game, the screen is black.

Game logic loop:

public void run(){
        long beforeTime = System.nanoTime();
        Canvas c = null;
            c = holder.lockCanvas(null);
            //synchronized (holder){

        this.sleepTime = delay-((System.nanoTime()-beforeTime)/1000000L);
        }catch(InterruptedException e){

Main activity:

protected void onCreate(Bundle savedInstanceState) {



engine.draw(Canvas c):

public void draw(Canvas canvas){
    Paint bluepaint = new Paint(Color.BLUE);
    canvas.drawRect(50,50,100,100, bluepaint);


@wondra I did test the break point, and the thread does actually get started, or so I think. The app runs and there are no errors thrown to the log console. Someone also suggested it was the emulator not running properly.

@fadden I have implemented what you suggested, still nothing... does starting the view from the xml and not from the Java have to do anything to do with it? Does the instance still exists if I don't declare it in Java?

  • \$\begingroup\$ Have you started the thread? Place breakpoint if your gameloop is ever reached. \$\endgroup\$
    – wondra
    Commented Jan 17, 2015 at 15:54
  • \$\begingroup\$ also, the gamme thread is started at my gameView class when the surface is created. \$\endgroup\$
    – KrackLocks
    Commented Jan 18, 2015 at 18:22
  • \$\begingroup\$ Some key points are missing: how you access the surfaceview? How do you start the thread?. Starting view from xml/java has nothing to do with anythin. \$\endgroup\$
    – fdreger
    Commented Nov 17, 2015 at 12:42
  • 1
    \$\begingroup\$ The black screen is how we all start. The hardest is getting the first sprite/triangle to show up. \$\endgroup\$
    – v.oddou
    Commented Mar 17, 2016 at 1:16
  • \$\begingroup\$ Where do you swap buffers at? \$\endgroup\$
    – Krythic
    Commented Nov 26, 2016 at 18:14

1 Answer 1


It looks like you're drawing on a SurfaceView surface. Bear in mind that the surface is a completely separate layer that, by default, sits behind everything else. The View part of the SurfaceView is a transparent "hole" that the layout code uses to adjust everything else around it. The most common way to get a black screen is to define an opaque background color for the View part.

A simple thing to try: use SurfaceView#setZOrderOnTop() to put the surface on top of everything else. You need to call this before the surface is created (onCreate()). If your rendering appears, then your render code is fine but your Views are obscuring your surface.

You may want to change your blackpaint to some other color (red) so you can see if any of it is visible.

Other tips: use Canvas#drawColor() to clear the screen, rather than a rectangle. Get rid of postInvalidate() -- by definition you are drawing on a separate layer and you do not need or want the Views to do anything. You don't need the synchronized (holder) either.

You may also want to read about game loops in the graphics architecture doc.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .