Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
游戏循环中onResume后出现Android空指针异常_Android_Onresume_Game Loop - Fatal编程技术网

游戏循环中onResume后出现Android空指针异常

游戏循环中onResume后出现Android空指针异常,android,onresume,game-loop,Android,Onresume,Game Loop,我正在开发一款android游戏。我和其他人面临着同样的问题 我尝试了等待并通知所有解决方案,如图所示。但一旦应用程序恢复,我就会得到空指针异常。 这是日志cat信息 INFO/System.out(8166): on resume INFO/System.out(8166): !!!! IN THE GAME RESUME ---- >>> INFO/System.out(8166): SURFACE CREATED INFO/System.out(8166): thre

我正在开发一款android游戏。我和其他人面临着同样的问题

我尝试了等待并通知所有解决方案,如图所示。但一旦应用程序恢复,我就会得到空指针异常。 这是日志cat信息

INFO/System.out(8166): on resume
INFO/System.out(8166): !!!! IN THE GAME RESUME ---- >>> 
INFO/System.out(8166):  SURFACE CREATED
INFO/System.out(8166): thread on resume
INFO/System.out(8166): in thread game pause=true
WARN/dalvikvm(8166): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
ERROR/AndroidRuntime(8166): FATAL EXCEPTION: Thread-11
ERROR/AndroidRuntime(8166): java.lang.NullPointerException
ERROR/AndroidRuntime(8166):     at com.org.GummyBlast.GameView.onDraw(GameView.java:442)
ERROR/AndroidRuntime(8166):     at com.org.GummyBlast.GameLoopThread.run(GameLoopThread.java:88)
onSurfaceCreated和onsurfacedestroming中的代码

private void init() {

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

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            System.out.println(" SURFACE DESTROYED");

            nullImages();
            boolean retry = true;

            //gameLoopThread.setRunning(false);
            if(!mGameIsRunning){
            while (retry) {
                try {
                    gameLoopThread.join();

                    retry = false;
                } catch (InterruptedException e) {
                }
            }
        }


    }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            System.out.println(" SURFACE CREATED");
            Log.d("surface create", "SURFACE CREATED");

            /*gameLoopThread.setRunning(true);

            gameLoopThread.start();*/
            start(holder);

        }

        public void start(SurfaceHolder holder) {
            if (!mGameIsRunning) {

                gameLoopThread.setRunning(true);
                gameLoopThread.start();
                mGameIsRunning = true;
            } else {

                gameLoopThread.onResume();
            }
        }
我的GameThread类中的代码

public class GameLoopThread extends Thread {
static final long FPS = 10;
private  GameView view;
public static boolean running = false;
public static boolean run = true;
public static boolean game_pause=true;
 private static Object mPauseLock;
 private boolean mFinished=false;
 long ticksPS = 1000 / FPS;
    long startTime;
    long sleepTime;
 SurfaceHolder holder;
public GameLoopThread(GameView view) {
     mPauseLock = new Object();

    this.view = view;
}

public GameLoopThread() {
     mPauseLock = new Object();
}


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




@Override
public void run() {
    while (running) {
        // Do stuff.
        System.out.println("in thread game pause="+Boolean.toString(game_pause));
        if (game_pause == true) {
            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) {
                System.out.println("Error in game loop thread "+ e.getMessage());

            }
        }
        else{
        synchronized (mPauseLock) {
            while (game_pause==false) {
                try {
                    mPauseLock.wait();
                } catch (InterruptedException e) {
                }
            }
        }
        } 
    }
}
public void onPause() {
    synchronized (mPauseLock) {
        game_pause = false;
        System.out.println("thread on pause");
    }
}

/**
 * Call this on resume.
 */
public void onResume() {
    synchronized (mPauseLock) {
        game_pause = true;
        mPauseLock.notifyAll();
        System.out.println("thread on resume");
    }
}
}

活动类暂停

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

    System.out.println(" --- IN GAME PAUSE ---");


        gameLoopThread = new GameLoopThread();
        gameLoopThread.onPause();

      }
如何在onResume中保持游戏状态?
谢谢

也许你需要在你的超类上调用
onResume()

public void onResume() {
     super.onResume();
}

希望这对您有所帮助。

我在我的类中调用了onResume,但我正在恢复GameView类中onSurfaceCreated方法中的线程。第442行是我的onDraw方法中的一行,第88行是view.onDraw(c);在我的线程类的while循环中,但是没有442行,哪一条线是准确的?每次我运行项目GameView.java:line没有变化,但GameLoopThread:line没有变化,即88。一旦我从菜单屏幕上单击开始按钮,游戏开始,画布绘制,但在我单击home按钮,然后继续游戏后,我在view.onDraw(c)上出现空指针异常;我回到菜单屏幕。从主屏幕恢复游戏后,如何保持画布状态。