Search code examples
androidcanvasonresume

canvas return null onResume method


im trying to develop a game app.I implement onResume and onPause methods. onPause method works fine however onResume method crushes.

 @Override
                public void surfaceCreated(SurfaceHolder holder) {
                    // TODO Auto-generated method stub
                    resume();
                }
public void resume()
    {
        addAnime();
        myThread.setRunning(true);
        myThread.start();
    }

if im not call update in onResume method myApp works fine but if i pause game and resume it again emulator says app not responding.

if i call resume in onResume method it says canvas is null at first runnig

@Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        view.resume();
    }

I dont understand why canvas is null at the begining of the app when i call resume in onResume.Someone explain me?

EDIT puase():

public void pause()
    {
        boolean repaet=true;
        while(repaet)
        {
            try {
                myThread.join();
                myThread.setRunning(false);
                repaet=false;
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        myThread=null;
    }

EDIT2 myThread

public void run() {
        long ticksPS = 1000 / FPS;
        long startTime;
        long sleepTime;
        while (running) {
            Canvas c = null;
            startTime = System.currentTimeMillis();
            try {
                c = view.getHolder().lockCanvas();
                synchronized (view.getHolder()) {
                    view.onDraw(c);
                }
            } finally {
                if (c != null) {
                    view.getHolder().unlockCanvasAndPost(c);
                }
            }
            sleepTime = ticksPS - (System.currentTimeMillis() - startTime);
            try {
                if (sleepTime > 0)
                    sleep(sleepTime);
                else
                    sleep(10);
            } catch (Exception e) {}
        }

EDIT3 here is my set runnig function

public void setRunning(boolean run) {
        running = run;
    }

Solution

  • Pause method:

    public void pause()
        {
            boolean repeat = myThread != null;
            while(repeat)
            {
                try {
                    myThread.setRunning(false);
                    myThread.join();
                    repeat=false;
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
            }
            myThread=null;
        }
    

    myThread:

    public class gameThread extends Thread { 
        private SurfaceView view;
        private boolean isRunning;
    
        private static final int FPS; // set FPS with your value
    
        private void sleep(long time) throws InterruptedException
        {
             Thread.sleep(time);
        }
    
        public void setRunning(boolean running)
        {
             synchronized(this)
             {
                 this.isRunning = running;
             }
        }
    
        public boolean isRunning()
        {
            synchronized(this)
            {
                 return this.isRunning;
            }
        }
    
        public void run()
        {
            long ticksPS = 1000 / FPS;
        long startTime;
        long sleepTime;
        while (isRunning()) {
            Canvas c = null;
            startTime = System.currentTimeMillis();
            try {
                c = view.getHolder().lockCanvas();
                synchronized (view.getHolder()) {
                    view.onDraw(c);
                }
            } finally {
                if (c != null) {
                    view.getHolder().unlockCanvasAndPost(c);
                }
            }
            sleepTime = ticksPS - (System.currentTimeMillis() - startTime);
            try {
                if (sleepTime > 0)
                    sleep(sleepTime);
                else
                    sleep(10);
            } catch (Exception e) {}
        }
    }
    

    Resume method:

    public void resume()
        {
            addAnime();
            // you must create a new thread. (You cannot use this same thread if it end)
            myThread = new gameThread(surfaceView);
            myThread.setRunning(true);
            myThread.start();
        }