Thursday 3 June 2010

IllegalThreadStateException in LunarLander, a sample Android code from Google

I think it can be classified as a bug in Lunar Lander game, in the SDK samples folder: /samples/LunarLander/:

Start the game and exit by pressing HOME key when the space ship is falling downing, then re-enter the game, it will be a IllegalThreadStateException thrown! and the App will be terminated.



My previous exercise("Android SurfaceView", "SurfaceView in a FrameLaout inside another LinearLayout" and "SurfaceView overlap with a LinearLayout") was implemented by reference to the sample code. Such that all of them inherit the bug.

My solution is to move the code "thread = new MySurfaceThread(getHolder(), this);" from constructor of the SurfaceView to inside the surfaceCreated() method.



From the video, it can be noted that if the App exit by HOME key, then re-enter, the onCreate() method of the Activity, and also the constructor of the SurfaceView will not be called, the App continuous from the previous state; so no object of the Thread will be instanced. That's why the IllegalThreadStateException thrown in the original approach.

But...if the App exit by BACK key, then re-enter, the onCreate() method of the Activity, and the constructor of the SurfaceView will be called, and will not have this problem.

The modified version of AndroidMergeSurfaceView can be downloaded here.





No comments:

Post a Comment