Android IllegalThreadStateException in LunarLander

半城伤御伤魂 提交于 2019-12-02 13:51:40

问题


Just come to polishing my application and making it resume after the user has left. When the application restores I get an IllegalThreadStateException, which is quite annoying. This problem is present in the example google gives of Lunar Lander. Has anyone found a way to restore working when using surfaceView?


回答1:


I believe this arises from a disparity in how the Surface and the Activity are handled. When you leave the LunarLander application the surface is destroyed (invoking surfaceDestroyed) but the Activity is only paused (invoking onPause). When the Activity is resumed the surface is created (invoking surfaceCreated) and it attempts to start the drawing thread again.

This means that creating the Thread happens with the Activity's lifecycle and destroying the thread happens with the SurfaceView's lifecycle, which do not always correspond, thus the IllegalThreadStateException. The solution would be to tie the thread to one lifecycle or the other, not both.

I think this thread proposes a possible solution, though I don't know if it works.




回答2:


In my own test, I create the drawing thread in the surfaceCreated() method, and this solves the issue completely. This is my method implementation:

@Override
public void surfaceCreated(SurfaceHolder arg0) {
    _thread = new DrawThread(getHolder());
    _thread.setRunning(true);
    _thread.start();
}



回答3:


So in the code, when surfaceDestroyed() is called, it sets mRun to false and calls thread.join(). This causes the thread to complete and die. When the app is started again and surfaceCreated() is called, it calls thread.start(). This is invalid because the thread can not be started after it dies.

Two options to fix:

a) Start a new thread in surfaceCreated() - as above.

b) Or add a check in surfaceDestroyed() against Activity.isFinishing() to only end the thread if true. To do this, I surrounded the while(mRun) in the thread with another while loop that is only set to false if isFinishing() returns true.



来源:https://stackoverflow.com/questions/683136/android-illegalthreadstateexception-in-lunarlander

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!