问题
I've been playing around with the lunar lander example and have run into a problem while trying to implement an about screen. I changed one of the menu items (in onCreateOptionsMenu) to an "about" option. When this option is selected, a new Activity starts and displays an about screen.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_ABOUT:
startActivity(new Intent(this, About.class));
return true;
//Other cases
}
return false;
}
This works perfectly and a the focus moves to a new Activity. However, when I press back on the phone to get back to the game, the program crashes. I get this error:
Thread [<3> main] (Suspended (exception IllegalThreadStateException))
LunarView(SurfaceView).updateWindow(boolean) line: 465
LunarView(SurfaceView).onWindowVisibilityChanged(int) line: 189
LunarView(View).dispatchWindowVisibilityChanged(int) line: 3782
FrameLayout(ViewGroup).dispatchWindowVisibilityChanged(int) line: 704
FrameLayout(ViewGroup).dispatchWindowVisibilityChanged(int) line: 704
PhoneWindow$DecorView(ViewGroup).dispatchWindowVisibilityChanged(int) line: 704
ViewRoot.performTraversals() line: 710
ViewRoot.handleMessage(Message) line: 1650
ViewRoot(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 4595
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 860
ZygoteInit.main(String[]) line: 618
NativeStart.main(String[]) line: not available [native method]
Can anyone help me figure out whats going on? I run into the same problem anytime I try to come back to the game screen from something else (whether its from a preferences Activity, a help Activity, or from the home screen). Thanks
回答1:
The link that fiction gave led me to the answer. The problem wasn't in my code, it was due to a flaw in the Lunar Lander example (it doesn't handle its thread properly).
For anyone looking for a stable game loop that doesn't use Lunar Lander's flawed framework, I found this example to work very well: http://blorb.tumblr.com/post/236799414/simple-java-android-game-loop
Thanks for the help
来源:https://stackoverflow.com/questions/4708198/flaw-in-the-lunar-lander-example-illegalthreadstateexception