【问题标题】:Exception caused by still running thread on back button press(Android)按下后退按钮时仍在运行线程导致的异常(Android)
【发布时间】:2010-09-21 16:24:31
【问题描述】:

我正在使用类似于下面的代码,但线程是通过按下按钮启动的。当我按下按钮时,会抓取 url 上的数据并将我切换到其他活动。我遇到的问题是,当我在其他活动中并点击后退按钮时,我得到一个异常,说线程已经启动。当活动加载检查 isAlive 时,我尝试杀死线程,但它似乎不会死。有谁碰巧知道我如何在创建的线程未运行的情况下将此活动恢复到其原始状态?

import java.io.BufferedInputStream;  
import java.io.InputStream;  
import java.net.URL;  
import java.net.URLConnection;  
import org.apache.http.util.ByteArrayBuffer;  

public class Iconic extends Activity {  
private String html = "";  
private Handler mHandler;  

public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.main);  
    mHandler = new Handler();  
    checkUpdate.start();  
}  

private Thread checkUpdate = new Thread() {  
    public void run() {  
        try {  
            URL updateURL = new URL("http://iconic.4feets.com/update");  
            URLConnection conn = updateURL.openConnection();  
            InputStream is = conn.getInputStream();  
            BufferedInputStream bis = new BufferedInputStream(is);  
            ByteArrayBuffer baf = new ByteArrayBuffer(50);  

            int current = 0;  
            while((current = bis.read()) != -1){  
                baf.append((byte)current);  
            }  

            /* Convert the Bytes read to a String. */  
            html = new String(baf.toByteArray());  
            mHandler.post(showUpdate);  
        } catch (Exception e) {  
        }  
    }  
};  

private Runnable showUpdate = new Runnable(){  
    public void run(){  
        Intent newIntent = New Intent(Iconic.this, otherClass.class);
        startActivity(newIntent);
    }  
};  

}

【问题讨论】:

    标签: android multithreading


    【解决方案1】:

    我的一个应用程序也遇到了类似的问题:我在另一个线程上做某事,当线程运行时,用户会切换到横向模式创建一个新的活动。一旦线程完成,应用程序将强制关闭,因为线程指向一个不再存在的活动。

    我对这个问题的解决方案是使用AsyncTask。基本上,它抽象了一个线程和一个处理程序,并允许您获取后台任务的定期进度更新(即实现类似ProgressBar 的东西)。我还从我的 Activity.onDestroy() 方法中调用了 AsyncTask.cancel(),因此当 Activity 被销毁时,AsyncTask 将停止运行。

    【讨论】:

      【解决方案2】:

      在这种情况下,您不必为启动活动创建新线程。 .而是

      这样做

      { 在您的跨线程中。

      progressHandler.sendMessage(progressHandler.obtainMessage());

      }

      处理程序进度处理程序=新处理程序(){ public void handleMessage(final Message msg) {

              Intent newIntent = New Intent(Iconic.this, otherClass.class);
          startActivity(newIntent);
      
         }
      };
      

      【讨论】:

        【解决方案3】:

        您不能在同一个 Thread 实例上调用 .start() 两次。您是否尝试过重新创建它?

        ...

        checkUpdate = new Thread(checkUpdateRunnable).start();
        

        ...

        private Runnable checkUpdateRunnable = new Runnable() {  
            public void run() {
        

        ...

        希望对您有所帮助, 再见!

        【讨论】:

          【解决方案4】:

          您应该在服务中运行线程。 This video(来自 Google I/O 2010)非常适合了解 Android 活动生命周期和应用程序架构中的服务是如何设计的。

          【讨论】:

            【解决方案5】:

            我遇到了同样的问题,但对于当前实例,我创建了以下解决方案。我知道这很尴尬,但是如果您知道正确的方法,请教我。还有一件事我读过,你不能停止 Java 中的线程,除非它包含一些未来的任务。比如 ExecutorService 线程……这是真的吗?

             public void surfaceCreated(SurfaceHolder holder) {
                    try{
                    _thread.setRunning(true);
                    _thread.start();
                    }catch(Exception ex){
                        _thread = new TutorialThread(getHolder(), this);
                        _thread.start();
                    }
            
                }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2016-11-02
              • 1970-01-01
              • 1970-01-01
              • 2014-01-02
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多