android - 运行一段时间后,SurfaceView破坏Surface

标签 android multithreading canvas surfaceview surfaceholder

我正在尝试创建一个运行gameloop线程的surfaceView,我花了很多时间试图解决运行一段时间后遇到的这个问题,游戏循环看起来还不错,它可以继续运行,并在以后继续引发错误第一次,我遇到了许多不同的错误,并试图跟踪到目前为止所做的一切,我得到的是我的 Canvas 一直返回null,它运行了一段时间并像它的假定那样绘制,但是随后返回null,我把System.out.println();在多个位置查看某些方法是否正在运行,以及它们运行了多少次(是的,我知道使用日志,但我并不真正喜欢它们),无论如何,我注意到的是,当我运行该应用程序时,它会运行onResume,onPause,然后再次onResume,然后在SurfaceView中被破坏,该应用程序实际上继续绘制,但一直将NPE扔到 Canvas 上。我不确定,但是当我什么都不做时,我不认为应该在运行时调用被破坏的表面。这是我所有代码的代码,包括Main:

package com.kojense.maverick.projectishgard.game;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.view.WindowManager;

public class Main extends AppCompatActivity {

    public static int COUNT = 0;

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

    @Override
    public void onPause(){
        super.onPause();
        System.out.println("Paused");
    }

    @Override
    public void onResume(){
        super.onResume();
        System.out.println("Resumed");
    }

    @Override
    public void onStop(){
        super.onStop();
        System.out.println("Stopped");
    }
}

GamePannel:
package com.kojense.maverick.projectishgard.game;

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

import com.kojense.maverick.projectishgard.R;

/**
 * Created by Maverick on 5/2/2017.
 */

public class GamePannel extends SurfaceView implements SurfaceHolder.Callback {

    private GameThread thread;

    public static int WIDTH, HEIGHT, SPEED = 15;

    private HUD hud;
    private block b;

    public GamePannel(Context context){
        super(context);
        getHolder().addCallback(this);
        thread = new GameThread(getHolder(), this);
        setFocusable(true);
    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {

        this.WIDTH = getWidth();
        this.HEIGHT = getHeight();

        hud = new HUD(BitmapFactory.decodeResource(getResources(), R.drawable.b1));
        b = new block();

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

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

        System.out.println("Destroyed");

    }

    @Override
    public boolean onTouchEvent(MotionEvent e){


        return super.onTouchEvent(e);
    }

    public void update(){

        b.update();

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

        canvas.drawColor(Color.WHITE);
        b.draw(canvas);

        hud.draw(canvas); //Keep at end to stay ontop
    }
}

游戏线程:
package com.kojense.maverick.projectishgard.game;

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

/**
 * Created by Maverick on 5/2/2017.
 */

public class GameThread extends Thread implements Runnable {

    public static final int MAX_FPS = 30;
    private SurfaceHolder holder;
    private GamePannel pannel;
    private boolean running = true;
    private Canvas canvas;

    public GameThread(SurfaceHolder holder, GamePannel pannel){
        super();
        this.holder = holder;
        this.pannel = pannel;
    }

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

    @Override
    public void run(){

        long startTime, waitTime, totalTime = 1000/MAX_FPS;

        while(running){
            startTime = System.currentTimeMillis();
            canvas = null;

            try {
                canvas = this.holder.lockCanvas();
                synchronized (holder) {
                    this.pannel.update();
                    this.pannel.draw(canvas);
                }
            }catch (Exception e){
                e.printStackTrace();
            } finally{
                if(canvas != null) {
                    try {
                        holder.unlockCanvasAndPost(canvas);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }

            waitTime = totalTime-(System.currentTimeMillis() - startTime);

            try{
                if(waitTime > 0) {
                    sleep(waitTime);
                } else {
                    sleep(10);
                }
            }catch (Exception e){}
        }
    }
}

错误日志:
05-04 17:05:59.030 3993-4428/com.kojense.maverick.projectishgard D/mali_winsys: new_window_surface returns 0x3000,  [2560x1440]-format:1
05-04 17:05:59.070 3993-3993/com.kojense.maverick.projectishgard D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=2
05-04 17:05:59.080 3993-3993/com.kojense.maverick.projectishgard I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@22f318b time:7154875
05-04 17:05:59.080 3993-3993/com.kojense.maverick.projectishgard I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@9eb69c6 time:7154875
05-04 17:05:59.080 3993-3993/com.kojense.maverick.projectishgard V/ActivityThread: updateVisibility : ActivityRecord{b5a4703 token=android.os.BinderProxy@9eb69c6 {com.kojense.maverick.projectishgard/com.kojense.maverick.projectishgard.splash.SplashScreen}} show : false
05-04 17:06:06.890 3993-3993/com.kojense.maverick.projectishgard I/Timeline: Timeline: Activity_launch_request id:com.kojense.maverick.projectishgard time:7162687
05-04 17:06:06.910 3993-3993/com.kojense.maverick.projectishgard I/System.out: Paused
05-04 17:06:06.920 3993-3993/com.kojense.maverick.projectishgard W/ResourcesManager: getTopLevelResources: /data/app/com.kojense.maverick.projectishgard-1/base.apk / 1.0 running in com.kojense.maverick.projectishgard rsrc of package com.kojense.maverick.projectishgard
05-04 17:06:06.930 3993-3993/com.kojense.maverick.projectishgard D/Activity: performCreate Call Injection manager
05-04 17:06:06.930 3993-3993/com.kojense.maverick.projectishgard I/InjectionManager: dispatchOnViewCreated > Target : com.kojense.maverick.projectishgard.game.Main isFragment :false
05-04 17:06:06.930 3993-3993/com.kojense.maverick.projectishgard I/System.out: Resumed
05-04 17:06:06.930 3993-3993/com.kojense.maverick.projectishgard D/SecWifiDisplayUtil: Metadata value : SecSettings2
05-04 17:06:06.930 3993-3993/com.kojense.maverick.projectishgard D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{8d8c1d6 I.E...... R.....ID 0,0-0,0}
05-04 17:06:06.950 3993-4428/com.kojense.maverick.projectishgard D/mali_winsys: new_window_surface returns 0x3000,  [2560x1440]-format:1
05-04 17:06:07.000 3993-3993/com.kojense.maverick.projectishgard D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=2
05-04 17:06:07.020 3993-3993/com.kojense.maverick.projectishgard I/System.out: Destroyed
05-04 17:06:07.030 3993-3993/com.kojense.maverick.projectishgard I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@b9191f1 time:7162823
05-04 17:06:07.100 3993-4456/com.kojense.maverick.projectishgard W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.Canvas.drawColor(int, android.graphics.PorterDuff$Mode)' on a null object reference
05-04 17:06:07.100 3993-4456/com.kojense.maverick.projectishgard W/System.err:     at android.view.SurfaceView.dispatchDraw(SurfaceView.java:451)
05-04 17:06:07.100 3993-4456/com.kojense.maverick.projectishgard W/System.err:     at android.view.View.draw(View.java:17479)
05-04 17:06:07.100 3993-4456/com.kojense.maverick.projectishgard W/System.err:     at android.view.SurfaceView.draw(SurfaceView.java:442)
05-04 17:06:07.100 3993-4456/com.kojense.maverick.projectishgard W/System.err:     at com.kojense.maverick.projectishgard.game.GamePannel.draw(GamePannel.java:72)
05-04 17:06:07.100 3993-4456/com.kojense.maverick.projectishgard W/System.err:     at com.kojense.maverick.projectishgard.game.GameThread.run(GameThread.java:39)
05-04 17:06:07.270 3993-3993/com.kojense.maverick.projectishgard I/System.out: Stopped
05-04 17:06:07.270 3993-3993/com.kojense.maverick.projectishgard V/ActivityThread: updateVisibility : ActivityRecord{eca5344 token=android.os.BinderProxy@22f318b {com.kojense.maverick.projectishgard/com.kojense.maverick.projectishgard.game.Main}} show : false

编辑:

我发现错误,我相信,三星设备会暂停应用程序并恢复它们,我在 friend 手机上尝试了此代码,而他没有暂停错误,然后我在另一台运行最新版本android且相同问题的三星设备上尝试了此错误(我正在测试的当前计算机是Running M)。我猜它什么时候破坏了Surfaceview并导致 Canvas 为空。所以我想如果有人需要帮助我,我只是想弄清楚如何暂停和恢复线程,还有谁知道手机何时能这样做并破坏了Surfaceview(运行surfacedestroyed方法),如果我需要重新创建表面或仅继续使用线程(继续线程)。谢谢伙计们!

最佳答案

将这行代码添加到surfaceDestroyed方法中。

@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

       boolean retry= true;
       thread.setRunning(false);
       while(retry){
         try{
              thread.join();
              retry=false;

            }catch(InterruptedException e){
               Log.e("GamePannel","Exception in surfaceDestroyed",e);
            }
      }
}

关于android - 运行一段时间后,SurfaceView破坏Surface,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43793540/

相关文章:

android - 我无法从 Android 的 Kitkat 版本的收件箱中删除短信

android - 在死线程上向处理程序发送消息 Google Admob 奖励视频

java - 捕获线程内的所有异常

android - 如何使用我的应用程序在 Android 4.0 设备上启用 USB 网络共享?

c++ - 多线程应用程序在内存使用率高时崩溃并出现错误 R6016 或 0xC0000005

c# - MongoDb Upsert死锁

c - 串行代码比在 C 中只使用一个线程慢得多?

javascript - HTML5 Canvas 使黑色透明

javascript - 如何用javascript向服务器发送数据

Javascript循环选择所有矩形