简体   繁体   English

SurfaceView和线程已启动异常

[英]SurfaceView and thread already started exception

Checking the LunarLander example it uses that code for resume the drawer thread: 检查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();
}

and this for end: 这是为了结束:

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) {
        }
    }
}

But when I execute the project, press the home button and resume the application it crashes with 但是,当我执行项目时,请按下主屏幕按钮,然后恢复崩溃的应用程序

 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)

I've seen this method for handling the background thread in other examples but it crashes. 我在其他示例中已经看到过这种用于处理后台线程的方法,但是它崩溃了。 What is the problem with it? 有什么问题吗?

Your thread is still running, you dont stop it correctly i guess. 您的线程仍在运行,我猜您没有正确停止它。 You have to interrupt your thread or thats how i solved it. 您必须中断您的线程或多数民众赞成我如何解决它。 So instead of using setRunning and a boolean to make your thread run you use something like this: 因此,与其使用setRunning和一个布尔值来使您的线程运行,不如使用以下代码:

to start it: 启动它:

public void surfaceCreated(SurfaceHolder holder) {
    thread.start();
}

In the thread: 在线程中:

public void run() {

    try {
        while (true) {
            // code here
        }
    }
    catch (InterruptedException e) {
         //disable stuff here
    }
}

And to stop it: 并停止它:

public void surfaceDestroyed(SurfaceHolder holder) {
    thread.interrupt();
}

I just typed this quickly but it should give you an idea. 我只是快速输入了一下,但是应该可以给您一个提示。

You can do it like this: 您可以这样做:

@Override
public void surfaceCreated(SurfaceHolder holder) {
    if (!thread.isAlive()) {
        thread.start();
    }
}

This is how I solved this issue. 这就是我解决此问题的方法。 In the surfaceCreated method, the state of the thread may have changed to TERMINATED. 在surfaceCreated方法中,线程的状态可能已更改为TERMINATED。 You need to create the thread a new. 您需要创建一个新线程。

@Override
 public void surfaceCreated(SurfaceHolder holder) {
     bgSurfaceThread = bgSurfaceThread.getState().equals(Thread.State.TERMINATED) ? bgSurfaceThread : new BackgroundSurfaceThread(BackgroundSurfaceView.this);
    bgSurfaceThread.setRunning(true);
   bgSurfaceThread.start();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM