Force Close Error
<p>I am new to android app, in my gaming app When i trying to click on my home screen(home) easy, medium, hard buttons then i am able to play the game after finishing the game my score is displayed on another screen(Score screen) in that screen one home i con is displayed when i click on that icon i displayed dialog box with Yes &amp; No button when i click on Yes Button i have to go to my home screen but some times it's working fine but some times it's giving force close error. So any one can help me to over come that problem.Below is my code and screen shots. </p>

<ol>
<li>MainGamePanel in this class 640 to 657 line numbers giving error &amp;  some times</li>
<li>MainThread in this class 69 line number giving error</li>
</ol>

<p>1) Class One Code </p>

<pre><code>public class MainGamePanel extends SurfaceView implements
SurfaceHolder.Callback {

    private static final String TAG = MainGamePanel.class.getSimpleName();

    private MainThread thread;

    /// private can[] can = new can[15];
    private ArrayList&lt;can&gt; _cans = new ArrayList&lt;can&gt;();
    private SoundManager mSoundManager;

    private int CAN_CRUSHED; // no. of crushed cane (for score)
    private Paint paint;
    private Paint bon;
    private Paint intro;
    private Paint crushed;
    private Paint l1;
    private Paint l2;
    private Paint earned;
    private Paint buttontext;
    private int alternate = 1;


    private int CONTINUE_CAN_CRUSHED = 0;
    private Map&lt;Integer, Bitmap&gt; _bitmapCache = new HashMap&lt;Integer, Bitmap&gt;();

    private int LEVEL_RUNNING_IS ;
    private int LEVEL_START_TIME=0;
    private int LEVEL_ELAPSED_TIME=0;
    private int LEVEL_CURRENT_TIME=0;
    private boolean LEVEL_COMPLETE=false;
    private boolean LEVEL_IS_PAUSED =false;
    private boolean DIALOG_SHOWN=false;
    private boolean HIGH_SCORE_DIALOG = false;
    private int TIME_LEFT =0;
    private boolean LEVEL_INITIALIZED = false;
    playButton playagain;
    playButton nextLevel;
    private int RANDOM_BACKGROUND = 0;
    Random random ;
    homeButton home;
    pauseButton pause;
    yesButton yes;
    noButton no;
    private int SCORE;
    private int LEVEL_SCORE =0;

    private int PAUSED_ELAPSE_TIME;
    private int PAUSE_START_TIME;
    score core;

    // all time related variables
    int state = 0;

    long startTime = 0;
    long currentTime = 0;
    long timeElapsed = 0;
    long timeRemaining = 0;
    long prevTimeRemaining = 0;
    Context ct;


    public MainGamePanel(Context context , int level) {
        super(context);
        this.ct = context;
        LEVEL_RUNNING_IS = level;
        getHolder().addCallback(this);
        fillBitmapCache();
        mSoundManager = new SoundManager();

        mSoundManager.initSounds(context);
        mSoundManager.addSound(1, R.raw.crush7);
        mSoundManager.addSound(2, R.raw.crush6);
        mSoundManager.addSound(3, R.raw.bad_can);
        mSoundManager.addSound(4, R.raw.bonus2);
        mSoundManager.addSound(5, R.raw.crush5);
        core = new score();
        CAN_CRUSHED = 0;
        paint = new Paint(); // used to write scores and timer
        bon = new Paint(); // used to write scores and timer
        intro = new Paint();
        crushed = new Paint();
        l1 = new Paint();
        l2 = new Paint();
        random =  new Random();
        earned = new Paint();
        buttontext = new Paint();



        crushed.setColor(Color.parseColor("#d00800"));
        crushed.setTextSize(28);
        crushed.setFakeBoldText(true);
        crushed.setAntiAlias(true);
        l1.setColor(Color.parseColor("#167b18"));
        l1.setTextSize(24);
        Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
        l1.setTypeface(font);
        l1.setAntiAlias(true);
        earned.setColor(Color.parseColor("#d00800"));
        earned.setTextSize(28);
        earned.setAntiAlias(true);
        earned.setFakeBoldText(true);
        buttontext.setColor(Color.parseColor("#ffffff"));
        buttontext.setTextSize(18);
        buttontext.setAntiAlias(true);
        buttontext.setTextAlign(Align.CENTER);



        playagain = new playButton(_bitmapCache.get(R.drawable.play),getWidth()/2,348);
        nextLevel =  new playButton(_bitmapCache.get(R.drawable.play),getWidth()/2,390);
        pause = new pauseButton(_bitmapCache.get(R.drawable.pause), getWidth()/2, getHeight()-20);
        home = new homeButton(_bitmapCache.get(R.drawable.home), 30, getHeight()-20);
        no = new noButton(_bitmapCache.get(R.drawable.dnobutton), 120, 290);
        yes = new yesButton(_bitmapCache.get(R.drawable.dyesbutton), 120, 290);
        RANDOM_BACKGROUND = Math.abs(random.nextInt() % 9);
        thread = new MainThread(  this);
        setFocusable(true);

    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
    }

    public void surfaceCreated(SurfaceHolder holder) {
    if (!thread.isAlive()) {
            thread = new MainThread(  this);

        }
        thread.setRunning(true);
        thread.start();
    }



    @Override
    public boolean onTouchEvent(MotionEvent event) {
        synchronized (getHolder())
        {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            // delegating event handling to the droid

            if(HIGH_SCORE_DIALOG)
            {

                yes.handleActionDown((int) event.getX(), (int) event.getY());
                no.handleActionDown((int) event.getX(), (int) event.getY());
                if(yes.isTouched())
                {
                     thread.setRunning(false);

                    thread.killme();
                     _bitmapCache.clear();




                     Intent myIntent = new Intent(ct, savescore.class);

                     Bundle bundle = new Bundle();
                     bundle.putString("SCORE",LEVEL_RUNNING_IS+"&amp;&amp;"+LEVEL_SCORE);

                     myIntent.putExtras(bundle);

                     ((Activity) getContext()).startActivity(myIntent);

                      yes.setTouched(false);    

                }
                if(no.isTouched())
                {
                    LEVEL_SCORE=-1;
                    HIGH_SCORE_DIALOG = false;

                }
            }
            if(DIALOG_SHOWN&amp;&amp;!HIGH_SCORE_DIALOG)
            {
                yes.handleActionDown((int) event.getX(), (int) event.getY());
                no.handleActionDown((int) event.getX(), (int) event.getY());
                if(yes.isTouched())
                {
                    thread.setRunning(false);

                     thread.killme();
                     _bitmapCache.clear();  
                     ((Activity) getContext()).finish();               

                      yes.setTouched(false);          

                }
                if(no.isTouched())
                {
                    DIALOG_SHOWN = false;

                    pause.setBitmap(_bitmapCache.get(R.drawable.pause));
                    LEVEL_IS_PAUSED=false;
                    LEVEL_START_TIME= LEVEL_START_TIME+PAUSED_ELAPSE_TIME;
                     no.setTouched(false);
                }

            }

            if(LEVEL_COMPLETE&amp;&amp;!HIGH_SCORE_DIALOG)
            {
                playagain.handleActionDown((int) event.getX(), (int) event.getY());


                home.handleActionDown((int) event.getX(), (int) event.getY());
                if(home.isTouched())
                {
                    if(!LEVEL_IS_PAUSED&amp;&amp;!DIALOG_SHOWN)
                    {

                    LEVEL_IS_PAUSED=true;
                    DIALOG_SHOWN = true;
                    pause.setTouched(false);
                    PAUSE_START_TIME = (int) (System.currentTimeMillis()/1000);
                    }else if(LEVEL_IS_PAUSED&amp;&amp;DIALOG_SHOWN)
                    {
//                      pause.setBitmap(_bitmapCache.get(R.drawable.pause));
//                      LEVEL_IS_PAUSED=false;
//                      LEVEL_START_TIME= LEVEL_START_TIME+PAUSED_ELAPSE_TIME;
//                      pause.setTouched(false);

                    } 

                }


                if(playagain.isTouched())
                {


                    RANDOM_BACKGROUND = Math.abs(random.nextInt() % 9);
                    LEVEL_INITIALIZED=false;
                    LEVEL_ELAPSED_TIME=0;
                    LEVEL_CURRENT_TIME=0;
                    LEVEL_START_TIME=0;
                    playagain.setTouched(false);
                    playagain.setBitmap(_bitmapCache
                            .get(R.drawable.play));
                    CAN_CRUSHED=0;
                    SCORE =0;
                    CONTINUE_CAN_CRUSHED=0;
                    _cans.clear();
                    canadded=false;

                    Log.i("PLAY    AGAIN IS TOUCHED","PLAY AGAIN IS TOUCHED ");

                }

                nextLevel.handleActionDown((int) event.getX(), (int) event.getY());
                if(nextLevel.isTouched())
                {

                    RANDOM_BACKGROUND = Math.abs(random.nextInt() % 9);
                    playagain.setBitmap(_bitmapCache
                            .get(R.drawable.play));
                    Log.i("next Level","Next level");
//                  nextLevel.setBitmap(_bitmapCache
//                          .get(R.drawable.play));

                    LEVEL_INITIALIZED=false;
                    LEVEL_ELAPSED_TIME=0;
                    LEVEL_CURRENT_TIME=0;
                    LEVEL_START_TIME=0;

                    LEVEL_SCORE=LEVEL_SCORE+CAN_CRUSHED;
                    SCORE =0;

                    CAN_CRUSHED=0;
                    CONTINUE_CAN_CRUSHED=0;
                    _cans.clear();
                    canadded=false;
                    Log.i("LEVEL RUNNING INSIDE NEXT CLICK",LEVEL_RUNNING_IS+"");


                    if(LEVEL_RUNNING_IS==11)
                    {

                        LEVEL_RUNNING_IS=12;
                        nextLevel.setTouched(false);
                    }
                    else if(LEVEL_RUNNING_IS==12)
                    {
                        LEVEL_RUNNING_IS=13;
                        nextLevel.setTouched(false);
                    }
                    else if(LEVEL_RUNNING_IS==13)
                    {

                        LEVEL_RUNNING_IS=21;
                        nextLevel.setTouched(false);
                    }
                    else if(LEVEL_RUNNING_IS==21)
                    {
                        LEVEL_RUNNING_IS=22;
                        nextLevel.setTouched(false);
                    }
                    else if(LEVEL_RUNNING_IS==22)
                    {
                        LEVEL_RUNNING_IS=23;
                        nextLevel.setTouched(false);
                    }
                    else if(LEVEL_RUNNING_IS==23)
                    {
                        LEVEL_RUNNING_IS=31;
                        nextLevel.setTouched(false);
                    }
                    else if(LEVEL_RUNNING_IS==31)
                    {
                        LEVEL_RUNNING_IS=32;
                        nextLevel.setTouched(false);
                    }
                    else if(LEVEL_RUNNING_IS==32)
                    {


                        LEVEL_RUNNING_IS=33;
                        nextLevel.setTouched(false);
                    }
                    else if(LEVEL_RUNNING_IS==33)
                    {

                        thread.killme();
                         _bitmapCache.clear();  
                         ((Activity) getContext()).finish();
                        nextLevel.setTouched(false);
                    }
                    nextLevel.setTouched(false);
                }


            }



            if(!LEVEL_COMPLETE)
            {

                pause.handleActionDown((int) event.getX(), (int) event.getY());
                home.handleActionDown((int) event.getX(), (int) event.getY());
                if(home.isTouched())
                {
                    if(!LEVEL_IS_PAUSED&amp;&amp;!DIALOG_SHOWN)
                    {

                    LEVEL_IS_PAUSED=true;
                    DIALOG_SHOWN = true;
                    pause.setTouched(false);
                    PAUSE_START_TIME = (int) (System.currentTimeMillis()/1000);
                    }else if(LEVEL_IS_PAUSED&amp;&amp;DIALOG_SHOWN)
                    {
//                      pause.setBitmap(_bitmapCache.get(R.drawable.pause));
//                      LEVEL_IS_PAUSED=false;
//                      LEVEL_START_TIME= LEVEL_START_TIME+PAUSED_ELAPSE_TIME;
//                      pause.setTouched(false);

                    } 

                }

                if(pause.isTouched())
                {
                    if(!LEVEL_IS_PAUSED)
                    {
                    pause.setBitmap(_bitmapCache.get(R.drawable.gplay));
                    LEVEL_IS_PAUSED=true;
                    pause.setTouched(false);
                    PAUSE_START_TIME = (int) (System.currentTimeMillis()/1000);
                    }else if(LEVEL_IS_PAUSED&amp;&amp;!DIALOG_SHOWN)
                    {
                        pause.setBitmap(_bitmapCache.get(R.drawable.pause));
                        LEVEL_IS_PAUSED=false;
                        LEVEL_START_TIME= LEVEL_START_TIME+PAUSED_ELAPSE_TIME;
                        pause.setTouched(false);

                    } 


                }

                if(!LEVEL_IS_PAUSED&amp;&amp;LEVEL_INITIALIZED)
                {

                for (can Can : _cans){
                    Can.handleActionDown((int) event.getX(), (int) event.getY());
                    if(Can.getwheelstep()==0&amp;&amp;Can.getcrushedstep()==0)
                    {

                    int ct ;
                    Boolean isdud = null;
                    if (Can.isTouched()) {
                        //  ct = i;
                        alternate++;
                        ct =    _cans.indexOf(Can);
                        if (Can.canisdud() == 0) {
                            Can.setBitmap(_bitmapCache
                                    .get(R.drawable.crushepgreen));
                            Can.setcrushedCanestep(2);
                            isdud = false;
                            mSoundManager.playSound(2);
                            CAN_CRUSHED++;
                            SCORE++;
                            CONTINUE_CAN_CRUSHED++;

                            if(CONTINUE_CAN_CRUSHED&gt;6)
                            {

                                if(Can.getwheelstep()==0)
                                    Can.sewheelrotatestep(1);
                                mSoundManager.playSound(4);
                                CAN_CRUSHED = CAN_CRUSHED+CONTINUE_CAN_CRUSHED;

                            }
                        }
                        if (Can.canisdud() == 1) {

                            Can.setBitmap(_bitmapCache
                                    .get(R.drawable.crushover));
                            Can.setcrushedCanestep(2);
                            mSoundManager.playSound(3);
                            isdud = true;
                            CONTINUE_CAN_CRUSHED=0;
                            CAN_CRUSHED--;
                        }
                        enableCans(ct, isdud);

                        // can[i].setTouched(false);
                    }
                    }
                }
                }

            }
        }

        if (event.getAction() == MotionEvent.ACTION_UP) {

            if(playagain.isTouched())
            {
                playagain.setBitmap(_bitmapCache
                        .get(R.drawable.play));
                RANDOM_BACKGROUND = Math.abs(random.nextInt() % 9);
                LEVEL_INITIALIZED=false;
                LEVEL_ELAPSED_TIME=0;
                LEVEL_CURRENT_TIME=0;
                LEVEL_START_TIME=0;
                playagain.setTouched(false);
                CAN_CRUSHED=0;
                SCORE =0;
                CONTINUE_CAN_CRUSHED=0;
                _cans.clear();
                canadded=false;

                Log.i("PLAY    AGAIN IS TOUCHED","PLAY AGAIN IS TOUCHED ");


            }
//          if(nextLevel.isTouched())
//          {
//              
//              Log.e("LEVEL   RRUNNING   IS",LEVEL_RUNNING_IS+"");
//              RANDOM_BACKGROUND = Math.abs(random.nextInt() % 9);
//              Log.i("next Level","Next level");
//              nextLevel.setBitmap(_bitmapCache
//                      .get(R.drawable.play));
//
//              LEVEL_INITIALIZED=false;
//              LEVEL_ELAPSED_TIME=0;
//              LEVEL_CURRENT_TIME=0;
//              LEVEL_START_TIME=0;
//              SCORE =0;
//
//              CAN_CRUSHED=0;
//              CONTINUE_CAN_CRUSHED=0;
//              _cans.clear();
//              canadded=false;
//              Log.i("LEVEL RUNNING INSIDE NEXT CLICK",LEVEL_RUNNING_IS+"");
//
//
//              if(LEVEL_RUNNING_IS==11)
//              {
//                  LEVEL_RUNNING_IS=12;
//                  nextLevel.setTouched(false);
//              }
//              else if(LEVEL_RUNNING_IS==12)
//              {
//                  LEVEL_RUNNING_IS=13;
//                  nextLevel.setTouched(false);
//              }
//              else if(LEVEL_RUNNING_IS==13)
//              {
//                  LEVEL_RUNNING_IS=21;
//                  nextLevel.setTouched(false);
//              }
//              else if(LEVEL_RUNNING_IS==21)
//              {
//                  LEVEL_RUNNING_IS=22;
//                  nextLevel.setTouched(false);
//              }
//              else if(LEVEL_RUNNING_IS==22)
//              {
//                  LEVEL_RUNNING_IS=23;
//                  nextLevel.setTouched(false);
//              }
//              else if(LEVEL_RUNNING_IS==23)
//              {
//                  LEVEL_RUNNING_IS=31;
//                  nextLevel.setTouched(false);
//              }
//              else if(LEVEL_RUNNING_IS==31)
//              {
//                  LEVEL_RUNNING_IS=32;
//                  nextLevel.setTouched(false);
//              }
//              else if(LEVEL_RUNNING_IS==32)
//              {
//                  LEVEL_RUNNING_IS=33;
//                  nextLevel.setTouched(false);
//              }
//              else if(LEVEL_RUNNING_IS==33)
//              {
//            
////                _bitmapCache.clear();
////                    ((Activity) getContext()).finish();
//           pausegamepanel();
////                    
//                  nextLevel.setTouched(false);
//              }
//              nextLevel.setTouched(false);
//          }


        }
        }
        return true;

}



    int levelstart;
    int tensecond=0;
    float co;
    float scoy;
    float timex;
    float timey;
    int[] gb = {R.drawable.gb1,R.drawable.gb2,R.drawable.gb3,R.drawable.gb4,R.drawable.gb5,R.drawable.gb6,R.drawable.gb7,R.drawable.gb8,R.drawable.gb9,R.drawable.gb10};



float money = 3.2f;



    public void render(Canvas canvas) {


        if(LEVEL_INITIALIZED)
        {

            if(LEVEL_ELAPSED_TIME==TIME_LEFT)
            {

                if(LEVEL_RUNNING_IS==13)
                {
                if(LEVEL_SCORE&gt;core.getlowesteasy(ct))
                {

                HIGH_SCORE_DIALOG = true;
                }
                }
                if(LEVEL_RUNNING_IS==23)
                {
                if(LEVEL_SCORE&gt;core.getlowestmedium(ct))
                {

                HIGH_SCORE_DIALOG = true;
                }
                }
                if(LEVEL_RUNNING_IS==33)
                {
                if(LEVEL_SCORE&gt;core.getlowesthard(ct))
                {

                HIGH_SCORE_DIALOG = true;
                }
                }


                switch(LEVEL_RUNNING_IS)
                {

                case 11 : canvas.drawBitmap(_bitmapCache.get(R.drawable.level1c1), 0, 0, null);
                break;
                case 12 : canvas.drawBitmap(_bitmapCache.get(R.drawable.level1c2), 0, 0, null);
                break;
                case 13 : canvas.drawBitmap(_bitmapCache.get(R.drawable.level1c3), 0, 0, null);
                break;
                case 21 : canvas.drawBitmap(_bitmapCache.get(R.drawable.level2c1), 0, 0, null);
                break;
                case 22 : canvas.drawBitmap(_bitmapCache.get(R.drawable.level2c2), 0, 0, null);
                break;
                case 23 : canvas.drawBitmap(_bitmapCache.get(R.drawable.level2c3), 0, 0, null);
                break;
                case 31 : canvas.drawBitmap(_bitmapCache.get(R.drawable.level3c1), 0, 0, null);
                break;
                case 32 : canvas.drawBitmap(_bitmapCache.get(R.drawable.level3c2), 0, 0, null);
                break;
                case 33 : canvas.drawBitmap(_bitmapCache.get(R.drawable.level3c3), 0, 0, null);
                break;


                }
                //canvas.drawBitmap(_bitmapCache.get(R.drawable.level1c1), 0, 0,
                //null);

                /*Paint p = new Paint();
                   Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
                    p.setTextSize( 18 );
                    p.setTypeface( font );
                    p.setAntiAlias(true);    */    

                 //  p.getTextBounds( HELLO_WORLD, 0, HELLO_WORLD.length(), textBounds );

                   //c.drawText( HELLO_WORLD, x, y, p );


                canvas.drawText(CAN_CRUSHED+"", 190, 100, crushed);
                canvas.drawText("You Recycled "+CAN_CRUSHED+" Cans"+"", 38, 130, l1);
                //money = (float) (Float.parseFloat(CAN_CRUSHED+"")*0.045); //*****
                money = (float) (Float.parseFloat(CAN_CRUSHED+"")*0.045);
                canvas.drawText(money+"", 215, 191, earned);

                //canvas.drawte



                playagain.setX(getWidth()/2);
                nextLevel.setX(getWidth()/2);
                playagain.draw(canvas);
                if(LEVEL_RUNNING_IS==33)
                {

                }
                else
                {
                nextLevel.draw(canvas);
                }
                canvas.drawText("Play Again", getWidth()/2, 354, buttontext);
                home.setX(50);
                home.setY(450);
                home.draw(canvas);

                switch(LEVEL_RUNNING_IS)
                {
                case 11 : canvas.drawText("Move to Easy Level 2", getWidth()/2, 397, buttontext);
                break;
                case 12 : canvas.drawText("Move to Easy Level 3", getWidth()/2, 397, buttontext);
                break;
                case 13 : canvas.drawText("Move to Medium Level 1", getWidth()/2, 397, buttontext);
                break;
                case 21 : canvas.drawText("Move to Medium Level 2", getWidth()/2, 397, buttontext);
                break;
                case 22 : canvas.drawText("Move to Medium Level 3", getWidth()/2, 397, buttontext);
                break;
                case 23 : canvas.drawText("Move to Hard  Level 1", getWidth()/2, 397, buttontext);
                break;
                case 31 : canvas.drawText("Move to Hard Level 2", getWidth()/2, 397, buttontext);
                break;
                case 32 : canvas.drawText("Move to Hard Level 3", getWidth()/2, 397, buttontext);
                break;
                case 33 : canvas.drawText("Move to Easy 1", getWidth()/2, 397, buttontext);
                break;

                }
                if(DIALOG_SHOWN)
                {

                    canvas.drawBitmap(_bitmapCache.get(R.drawable.dialoghome), getWidth()/2-(_bitmapCache.get(R.drawable.dialoghome).getWidth()/2), getHeight()/2-(_bitmapCache.get(R.drawable.dialoghome).getHeight()/2), null);
                    no.setX(200);
                    no.draw(canvas);
                    yes.draw(canvas);
                }

                if(HIGH_SCORE_DIALOG)
                {

                    canvas.drawBitmap(_bitmapCache.get(R.drawable.high), getWidth()/2-(_bitmapCache.get(R.drawable.dialoghome).getWidth()/2), getHeight()/2-(_bitmapCache.get(R.drawable.high).getHeight()/2), null);
                    no.setX(200);
                    no.draw(canvas);
                    yes.draw(canvas);

                }
                LEVEL_COMPLETE=true;
                LEVEL_INITIALIZED=true;
            }
</code></pre>

<p>2) Class 2 Code</p>

<pre><code>public class MainThread extends Thread {

    private static final String TAG = MainThread.class.getSimpleName();

    // Surface holder that can access the physical surface
    private SurfaceHolder surfaceHolder;
    // The actual view that handles inputs
    // and draws to the surface
    private MainGamePanel gamePanel;

    // flag to hold game state 
    private boolean running;
    public void setRunning(boolean running) {
        this.running = running;
    }

    //public MainThread(SurfaceHolder surfaceHolder, MainGamePanel gamePanel) 
    public MainThread(  MainGamePanel gamePanel){
        super();
        //this.surfaceHolder = surfaceHolder;
        this.gamePanel = gamePanel;
    }


    public void killme()
    {

        running=false;
        System.gc();
    }

    @Override
    public void run() {
        Canvas canvas;
        Log.d(TAG, "Starting game loop");
        while (running) {


            canvas = null;
            // try locking the canvas for exclusive pixel editing
            // in the surface
            try {
                canvas = gamePanel.getHolder().lockCanvas();
                synchronized (gamePanel.getHolder())
                {
                    // update game state 

                //  this.gamePanel.update();
                    // render state to the screen
                    // draws the canvas on the panel

                    this.gamePanel.initialise(canvas);
                    this.gamePanel.render(canvas);              
                }
            }   finally {
                // in case of an exception the surface is not left in 
                // an inconsistent state
                if (canvas != null) {
                    gamePanel.getHolder().unlockCanvasAndPost(canvas);
                }
            }   // end finally
        }
    }

}
</code></pre>
