android shut-down errors / thread problems
- by iQue
Im starting to deal with some stuff in my game that I thought were "minor problems" and one of these are that I get an error every time I shut down my game that looks like this:
09-05 21:40:58.320: E/AndroidRuntime(30401): FATAL EXCEPTION: Thread-4898
09-05 21:40:58.320: E/AndroidRuntime(30401): java.lang.NullPointerException
09-05 21:40:58.320: E/AndroidRuntime(30401): at nielsen.happy.shooter.MainGamePanel.render(MainGamePanel.java:94)
09-05 21:40:58.320: E/AndroidRuntime(30401): at nielsen.happy.shooter.MainThread.run(MainThread.java:101)
on these lines is my GameViews rendering-method and the line in my Thread that calls my GameViews rendering-method.
Im guessing Something in my surfaceDestroyed(SurfaceHolder holder) is wrong, or maybe Im not ending the tread in the right place.
My surfaceDestroyed-method:
public void surfaceDestroyed(SurfaceHolder holder) {
Log.d(TAG, "Surface is being destroyed");
boolean retry = true;
((MainThread)thread).setRunning(false);
while (retry) {
try {
((MainThread)thread).join();
retry = false;
} catch (InterruptedException e) {
}
}
Log.d(TAG, "Thread was shut down cleanly");
}
Also, In my activity for this View my onPaus, onDestoy and onStop are empty, do I maybe need to add something there?
The crash occurs when I press my home-button on the phone, or any other button that makes the game stop. But as I understand it the onPaus is called when you press the Home-button.. This is really new territory for me so Im sorry if im missing something obvious or something really big.
adding my surfaceCreated method asweel since that is where I start this thread:
public void surfaceCreated(SurfaceHolder holder) {
controls = new GameControls(this);
setOnTouchListener(controls);
timer1.schedule(new Task(this), 0, delay1);
thread.setRunning(true);
thread.start();
}
and aslong as this is running, my game is rendering and updating.