按下后退或菜单按钮后我的游戏死机/崩溃

时间:2017-12-13 15:54:49

标签: java android button android-activity

我正在开发一款Android游戏,我在这里遇到了一个奇怪的问题。每次我按回/菜单/任务按钮,我试图在游戏中返回,游戏冻结(没有响应(等待/关闭))。我甚至添加了onPause,onResume,onStop函数,但问题仍然存在。 我的代码可能存在问题,这是我的MainActivitiy。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    Constants.SCREEN_WIDTH = dm.widthPixels;
    Constants.SCREEN_HEIGHT = dm.heightPixels;
    setContentView(new GamePanel(this));

    String a = getApplicationInfo().dataDir;
    GamePlayScene.filePath = a;
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState){
    super.onSaveInstanceState(savedInstanceState);
}

@Override
public void onPause(){
super.onPause();
}

@Override
public void onResume(){
    super.onResume();
}
@Override
public void onStart(){
    super.onStart();
}
@Override
public void onStop(){
    super.onStop();
}

这是我的MainThread

public class MainThread extends Thread{
public static final int MAX_FPS = 30;
private double avarageFPS;
private SurfaceHolder surfaceHolder;
private GamePanel gamePanel;
private boolean running;
public static Canvas canvas;

public void setRunning(boolean running){
    this.running = running;
}
public MainThread(SurfaceHolder surfaceHolder, GamePanel gamePanel){
    super();
    this.surfaceHolder = surfaceHolder;
    this.gamePanel = gamePanel;
}
@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.gamePanel.update();
                this.gamePanel.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) {
            avarageFPS = 1000 / ((totalTime / frameCount) / 1000000);
            frameCount = 0;
            totalTime = 0;
            System.out.println(avarageFPS);
        }
    }
}

在游戏开始时我得到了这个,但这不会对游戏造成任何问题:

12-14 00:19:41.967 6778-6778/com.apps.lazy.rainbowjet E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from GradienCache 12-14 00:19:41.967 6778-6778/com.apps.lazy.rainbowjet E/OpenGLRenderer: MAX_TEXTURE_SIZE: 16384 12-14 00:19:41.971 6778-6778/com.apps.lazy.rainbowjet E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from Caches::initConstraints() 12-14 00:19:41.971 6778-6778/com.apps.lazy.rainbowjet E/OpenGLRenderer: MAX_TEXTURE_SIZE: 16384

按下任何按钮后,我在游戏中返回,我得到了这个:

12-14 00:26:17.174 12130-12163/com.apps.lazy.rainbowjet W/System.err: at android.view.SurfaceView.dispatchDraw(SurfaceView.java:343) 12-14 00:26:17.174 12130-12163/com.apps.lazy.rainbowjet W/System.err: at android.view.View.draw(View.java:14468) 12-14 00:26:17.174 12130-12163/com.apps.lazy.rainbowjet W/System.err: at android.view.SurfaceView.draw(SurfaceView.java:334) 12-14 00:26:17.174 12130-12163/com.apps.lazy.rainbowjet W/System.err: at com.apps.lazy.rainbowjet.GamePanel.draw(GamePanel.java:66) 12-14 00:26:17.174 12130-12163/com.apps.lazy.rainbowjet W/System.err: at com.apps.lazy.rainbowjet.MainThread.run(MainThread.java:43)

这是GamePanel

public class GamePanel extends SurfaceView implements SurfaceHolder.Callback 
{
private MainThread thread;

private SceneManager manager;
private boolean isRunning = true;

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


    getHolder().addCallback(this);
    Constants.CURRENT_CONTEXT = context;
    thread = new MainThread(getHolder(), this);
    manager = new SceneManager();
    setFocusable(true);
}

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

@Override
public void surfaceCreated(SurfaceHolder holder){
    thread = new MainThread(getHolder(), this);
    thread.setRunning(true);
    thread.start();
}

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

@Override
public boolean onTouchEvent(MotionEvent event) {
    manager.recieveTouch(event);
            return true;
        //return super.onTouchEvent(event);
}

public void update(){
manager.update();
}

@Override
public void draw(Canvas canvas){
    super.draw(canvas);
    manager.draw(canvas);
}
}

1 个答案:

答案 0 :(得分:0)

好的,首先,您添加了onPauseonResumeonStop,但您所做的一切都是调用super函数,所以基本上覆盖是无用。在这些函数中,您必须调用super并实际执行其他操作。

其次,如果问题出在后面的菜单中,你应该在onBackPressed函数中处理它。

在MainActivity中试试这个:

@Override
public void onBackPressed() {
    MainThread myThread = getMainThread();
    myThread.setRunning(false);
    super.onBackPressed();
}

其中getMainThread()检索活动中线程的引用(无论它在哪里)