存在未捕获异常的线程

时间:2013-10-07 18:32:26

标签: java android multithreading exception

首先,我知道在stackoverflow上已经发布了类似的问题,但在这种情况下我似乎无法找到确切的问题。

我在logcat中收到一些错误消息,整体而言我的程序非常不稳定,常常会意外关闭。

10-07 180852.056 EAndroidRuntime(1043) FATAL EXCEPTION Thread-85

10-07 180852.056 EAndroidRuntime(1043) java.lang.NullPointerException

10-07 180852.056 EAndroidRuntime(1043)  at com.ophion.td2.GameEngine.Draw(GameEngine.java61)

10-07 180852.056 EAndroidRuntime(1043)  at com.ophion.td2.GameThread.run(GameThread.java46)

10-07 180854.046 IChoreographer(1043) Skipped 49 frames!  The application may be doing too much work on its main thread. The application may be doing too much work on its main thread.

10-07 180855.026 IProcess(1043) Sending signal. PID 1043 SIG 9'

这是logcat,这里是我看到的问题代码文件(我可能错了!)。游戏引擎:

public class GameEngine implements Runnable{

public int screenWidth;
public int screenHeight;
private Paint whitePaint;
private Paint textPaint;
/* private String currentTimeString */

Blop blop;
private Context context;

public void Init(Context context) {
    this.context = context;
    Resources resources = context.getResources();

    whitePaint = new Paint();
    whitePaint.setColor(Color.WHITE);
    whitePaint.setStyle(Style.FILL);

    textPaint = new Paint();
    textPaint.setColor(Color.LTGRAY);
    textPaint.setTextSize(40);

    blop = new Blop(resources, 20, 20);
    blop.setDirection(300);

    setSurfaceDimensions(240, 160);


}

public void onDestroy() {
    try {
    }   catch (Exception e) {

    }
}

public void setSurfaceDimensions(int width, int height) {
    screenWidth = width;
    screenHeight = height;
}

public void Update() {
    /*currentTimeString = new SimpleDateFormat("HH:mm:ss").format(new Date()); */
    blop.move(screenWidth, screenHeight);
}

public void Draw(Canvas canvas) {
    canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), whitePaint);
    /* canvas.drawText(currentTimeString, 30, 100, textPaint); */
    blop.draw(canvas, blop.x, blop.y, textPaint);
}

@Override
public void run() {
    // TODO Auto-generated method stub

}

}    

游戏线程

public class GameThread extends Thread {
private SurfaceHolder surfaceHolder;
private Canvas canvas;
private long delay = 1000000000L / 25;
private long beforeTime = 0;
private long afterTime = 0;
private long timeDiff = 0;
private long sleepTime;
private long overSleepTime = 0;
private long excess = 0;

public final static int RUNNING = 1;
public final static int PAUSED = 2;
public static final int MAX_FRAME_SKIPS = 5;
int state = RUNNING;

GameEngine gameEngine;

public GameThread(SurfaceHolder surfaceHolder, Context context,
    Handler handler, GameEngine gameEngine) {
    this.surfaceHolder = surfaceHolder;
    this.gameEngine = gameEngine;
}

@Override
public void run() {

    while (state == RUNNING) {
        beforeTime = System.nanoTime();

        gameEngine.Update();

        canvas = null;
        try {
            canvas = surfaceHolder.lockCanvas(null);
            synchronized (surfaceHolder) {
                gameEngine.Draw(canvas);
            }
        } finally {
            if (canvas != null) {
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }

        afterTime = System.nanoTime();
        timeDiff = afterTime - beforeTime;
        sleepTime = ((delay) - timeDiff) - overSleepTime;

        if (sleepTime > 0) {
            try {
                sleep(sleepTime / 1000000L);
            } catch (InterruptedException ex) {
            }
            overSleepTime = (System.nanoTime() - afterTime) - sleepTime;
        } else {
            excess -= sleepTime;
            overSleepTime = 0L;
        }

        int skips = 0;
        while ((excess > delay) && (skips < MAX_FRAME_SKIPS)) {
            excess -= delay;
            gameEngine.Update();
            skips++;
        }
    }
}
}

我希望你能帮助我!

2 个答案:

答案 0 :(得分:0)

运行gameEngine

时,Thread似乎为空

答案 1 :(得分:0)

我不想计算你的行号,但是你在GameEngine的Draw()方法中得到了NullPointerException。这几乎肯定意味着当Thread调用该方法时,Canvas为null,或者blop为null。第61行的任何一个是罪魁祸首。