AHA!

I *knew* I was doing everything just like LunarLander did.  I was!
That was the problem.

This particular bug was really bothering me.  I became a little
obsessed with it, so I tried logging practically everything that my
game did while it was running.  After lots of painstaking work, I
finally discovered the code that was to blame.

This is from my game's overridden SurfaceView class:

public void surfaceCreated(SurfaceHolder holder) {
   gameThread.setRunning(true);
   gameThread.start();
}

You'll notice that this method matches the corresponding method in
LunarView exactly.

The first statement in the method executes fine.  The
IllegalThreadStateException is thrown when gameThread.start() is
called.  I looked it up in the Java API and found out that this is
happening because gameThread is already in the Thread.State.TERMINATED
state when the statement is called.  The exception only occurs when
the game is being restored, because when the game is starting up for
the first time, gameThread is in the Thread.State.NEW state.

This didn't add up.  My program was doing everything just like the
example program that Google supplied.  Well, I checked, and it turns
out that the LunarLander program exhibits the same bug!

'Don't believe me?  Try it for yourself.  Send yourself a test text-
message, but don't open it.  Then open LunarLander.  While the game is
in progress, drag down the top menu bar, and click on the notification
that you have received a text.  This will open a new Activity and
cause LunarLander to save its state in the background.  Once you have
finished reading your text-message, press the 'back' button to resume
LunarLander.  This will cause LunarLander to attempt to restore its
state.  You should see an error message.

The same thing should also happen when someone calls you in the middle
of the game, or any time that another Activity is opened on top of the
game's Activity.

I've been trying to come up with some sort of workaround.  Perhaps
before calling gameThread.start(), I could check to make sure that
gameThread.getState() == Thread.State.NEW .  This works fine, but it
still leaves the problem of how to restart gameThread if the state is
not NEW.  I suppose I could just set gameThread to a new GameThread()
and try to start that, but that approach won't work because I need to
supply the Context for the GameThread, and that isn't passed to
surfaceCreated().

Is Google aware of this bug?  Does anybody know how to fix it?  Once
again, any help is greatly appreciated.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to