androidandroid-canvasandroid-viewgame-loopandroid-screen

canvas is black after restart or debugg while running


When I restart my App, I mean when I press the Homebutton and start it from the Task window, or when I debug it while it is still runnning the whole screen went black!

This is my MainView:

package net.kex.toll;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;


public class MainView extends SurfaceView implements SurfaceHolder.Callback{

public GameLoop gameLoop;
public Player player;
private int playerX = 50;
private int playerY = 50;

public MainView(final Context context) {
    super(context);
    getHolder().addCallback(this);
    player = new Player();
    this.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            playerX = playerX + 20;
            playerY = playerY + 20;
            player.play();
            return false;
        }
    }
    );
    setFocusable(true);

}


public void surfaceChanged(SurfaceHolder surfaceHolder, int foramt, int width, int height){

}
public void surfaceDestroyed(SurfaceHolder surfaceHolder){
    boolean retry = true;
    while (true) {
        try {
            gameLoop.setRunning(false);
            gameLoop.join();
        } catch (Exception e) { e.printStackTrace();}
        retry = false;
    }
}
public void surfaceCreated(SurfaceHolder surfaceHolder){
    gameLoop = new GameLoop(getHolder(), this);

    gameLoop.setRunning(true);
    gameLoop.start();
}
public void update(){
    player.update();
}
public void draw(Canvas canvas){
    if(canvas == null) {
        return;
    }
    canvas.drawColor(Color.WHITE);
    player.draw(canvas);
}
}

This is my GameLoop:

package net.kex.toll;

import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;


public class GameLoop extends Thread {
public static final int MAX_FPS = 30;
private double averageFPS;
private MainView mainView;
private SurfaceHolder surfaceHolder;
private boolean running;
public static Canvas canvas;

public void setRunning(boolean running) {
    this.running = running;
}

public GameLoop(SurfaceHolder surfaceHolder, MainView mainView) {
    super();
    this.surfaceHolder = surfaceHolder;
    this.mainView = mainView;
}

@Override
public void run(){
    long startTime;
    long timeMillis = 1000/MAX_FPS;
    long waitTime;
    long frameCount = 0;
    long totalTime = 0;
    long targetTime = 1000/MAX_FPS;

    while (running) {
        startTime = System.nanoTime();
        canvas = null;
        try {
            canvas = this.surfaceHolder.lockCanvas();
            synchronized (surfaceHolder) {
                this.mainView.update();
                this.mainView.draw(canvas);
            }
        }catch (Exception e){ e.printStackTrace();}
        finally {
            if (canvas != null) {
                try {
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }catch (Exception e){ e.printStackTrace();}

            }
        }
        timeMillis = (System.nanoTime() - startTime)/1000000;
        waitTime = targetTime - timeMillis;
        try {
            if (waitTime >= 0){
                this.sleep(waitTime);
            }
        }catch (Exception e){ e.printStackTrace();}

        totalTime += System.nanoTime() - startTime;
        frameCount++;

        if (frameCount == MAX_FPS) {
            averageFPS = 1000/((totalTime/frameCount)/1000000);
            frameCount = 0;
            totalTime = 0;
            Log.i("GameLoop", "averageFPS: " + Double.toString(averageFPS));
        }
    }
}

}

I don't know what to do! If you want some other class please tell me and I will send it to you!

Please help I´m sitting here and trying to solve this problem for hours!


Solution

  • Repaint the object, it will render it again. For example in onResume add Repaint()