0

Every time I minimize my App it instantly crashes. The Error is the following: Also stackoverflow demands i add more than just code so here is some text. Also stackoverflow demands i add more than just code so here is some text. Also stackoverflow demands i add more than just code so here is some text. Also stackoverflow demands i add more than just code so here is some text. Also stackoverflow demands i add more than just code so here is some text. Also stackoverflow demands i add more than just code so here is some text.

2021-03-19 22:12:11.243 6919-7009/com.example.zawarudo E/AndroidRuntime: FATAL EXCEPTION: Thread-2
    Process: com.example.zawarudo, PID: 6919
    java.lang.IllegalStateException: No recording in progress, forgot to call #beginRecording()?
        at android.graphics.RenderNode.endRecording(RenderNode.java:404)
        at android.view.Surface$HwuiContext.unlockAndPost(Surface.java:1029)
        at android.view.Surface.unlockCanvasAndPost(Surface.java:418)
        at android.view.SurfaceView$2.unlockCanvasAndPost(SurfaceView.java:1567)
        at com.example.zawarudo.GameLoopThread.run(GameLoopThread.java:53)

Is there some error in my Thread logic?

public class GameLoopThread extends Thread {
    private GameView view;
    private boolean running = false;

    public GameLoopThread(GameView view){

        this.view = view;
    }
    public void setRunning(boolean run){
        running = run;
    }
    
    @Override
    public void run(){
            Canvas c = null;
    
            while (running){
    
                try {
                    currentThread().sleep(17);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
    
                try{
                    view.getHolder().setFixedSize(1080,1920);
                    c = view.getHolder().lockHardwareCanvas();
                                     
                    c.rotate(90);
                    synchronized (view.getHolder()){
                        view.onDraw(c);
                    }
                } finally {
                    {
    
        view.getHolder().unlockCanvasAndPost(c);
    
                            }
                        }
                    }
                }
            }




gameThread = new GameLoopThread(this);
            holder = getHolder();
            holder.addCallback(new SurfaceHolder.Callback() {
 
                @Override
                public void surfaceCreated(SurfaceHolder surfaceHolder) {
                    gameThread.setRunning(true);
                    gameThread.start();
                }
    
                @Override
                public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    
                }
    
                public void surfaceChanged(SurfaceHolder holder) {
    
                }
    
    
                @Override
                public void surfaceDestroyed(SurfaceHolder holder) {
                    boolean retry = true;
                    gameThread.setRunning(false);
                    while (retry) {
                        try {
                            gameThread.join();
                            retry = false;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }





public class GameView extends SurfaceView implements zaInterface {

    public GameMap tileMap = new GameMap();
    private SurfaceHolder holder;
    private GameLoopThread gameThread;

    public GameView(Context context) {
        super(context);

        screenheight = Resources.getSystem().getDisplayMetrics().heightPixels;;
        screenwidth = Resources.getSystem().getDisplayMetrics().widthPixels;

    
        gameThread = new GameLoopThread(this);
        holder = getHolder();
        holder.addCallback(new SurfaceHolder.Callback() {

            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                if (hasStarted==false) {
                    gameThread.setRunning(true);
                    gameThread.start();
                    hasStarted=true;
                }
                else{

                }
            }



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

            }

            public void surfaceChanged(SurfaceHolder holder) {

            }



            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                /*
                boolean retry = true;
                gameThread.setRunning(false);
                while (retry) {
                    try {
                        gameThread.join();
                        retry = false;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                 */
            }
        });
    }

And here is my Gameactivity Class

public class GameActivity extends AppCompatActivity {

    private GameView GameView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().isActive();
        GameMap.initialize();
        GameView.initialize();
        setContentView(new GameView(this));
    }
}

1 Answers1

0

Try to stop the SurfaceView when the activity is paused. using stopView()

@Override
protected void onPause() {
    super.onPause();
    if (gameThread != null)
        gameThread.setRunning(false);
}
Zain
  • 37,492
  • 7
  • 60
  • 84
  • I'm not sure how to implement this, if I try to add it to the other @Overrides in the given code i get the error "Method does not override method from its superclass". How would I trigger StopView/Get notified of the pause event? – FiveJungYetNoSmite Mar 19 '21 at 22:47
  • @FiveJungYetNoSmite can you show more code.. specifically how you define `view` object – Zain Mar 19 '21 at 22:49
  • I expanded my post. All relevant parts should be there. I currently tried editing the surfaceCreated method and emptying the surfacedestroyed method so it doesnt stop the App when tabbed out but that doesnt seem to work either. Thanks for trying to help me, i appreciate it – FiveJungYetNoSmite Mar 19 '21 at 23:06
  • Also if you are interested in what the game looks like here it is: https://old.reddit.com/r/UI_Design/comments/lyflbx/some_of_you_may_remember_my_first_draft_of_my/ – FiveJungYetNoSmite Mar 19 '21 at 23:07
  • I now figured out I have to place @Override public void onPause() { super.onPause(); GameView.stop(); } Into the GameActivity Class, but I get a null object reference error. Hm. – FiveJungYetNoSmite Mar 19 '21 at 23:22
  • @FiveJungYetNoSmite my bad please add `gameThread.setRunning(false);` instead in `onPause` – Zain Mar 19 '21 at 23:22