检查 LunarLander 示例,它使用该代码恢复抽屉线程:
public void surfaceCreated(SurfaceHolder holder) {
// start the thread here so that we don't busy-wait in run()
// waiting for the surface to be created
thread.setRunning(true);
thread.start();
}
最后:
public void surfaceDestroyed(SurfaceHolder holder) {
// we have to tell thread to shut down & wait for it to finish, or else
// it might touch the Surface after we return and explode
boolean retry = true;
thread.setRunning(false);
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
但是当我执行项目时,按下主页按钮并恢复它崩溃的应用程序
java.lang.IllegalThreadStateException: Thread already started.
at java.lang.Thread.start(Thread.java:1045)
at com.example.android.lunarlander.LunarView.surfaceCreated(LunarView.java:862)
at android.view.SurfaceView.updateWindow(SurfaceView.java:533)
at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:226)
at android.view.View.dispatchWindowVisibilityChanged(View.java:5839)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945)
我在其他示例中看到了这种处理后台线程的方法,但它崩溃了。它有什么问题?