0

我已经通过调用finish()销毁了我的活动。但是在销毁活动后活动中的线程仍在运行,并导致应用程序崩溃。我该如何处理?为什么线程在销毁活动后运行?请提供一些帮助?

class DBThread extends Thread {

    @Override
    public void run() {
        while (finish) {
            Cursor cursor = null;

            Cursor mCursor = lrDB.selectUserDetails(mUser_Id);
            if (mCursor.getCount() > 0) {
                mCursor.moveToNext();
                String Name = mCursor .getString(0);
                String userName = mCursor .getInt(1);
                String password = mCursor .getString(3);

            }
            mCursor.close();

        }
    }
}
4

6 回答 6

3

首先更正您的线程:

class DBThread extends Thread { 
    @Override 
    public void run() { 
        while (!isInterrupted()) {
            Cursor mCursor = lrDB.selectUserDetails(mUser_Id); 
            if (mCursor.getCount() > 0) { 
                mCursor.moveToNext(); 
                String Name = mCursor .getString(0); 
                String userName = mCursor .getInt(1); 
                String password = mCursor .getString(3); 

            } 
            mCursor.close(); 
        } 
    } 
} 

其次要优雅地关闭线程,在活动中执行类似的操作:

@Override
public void onDestroy() {
    if (yourThread!=null) {
        yourThread.interrupt(); // request to terminate thread in regular way
        yourThread.join(500); // wait until thread ends or timeout after 0.5 second
        if (yourThread.isAlive()) {
            // this is needed only when something is wrong with thread, for example hangs in ininitive loop or waits to long for lock to be released by other thread.
            Log.e(TAG, "Serious problem with thread!");
            yourThread.stop();
        }
    }
    super.onDrestroy();
}

请注意, Thread.destroy() 已被弃用并且将不起作用。

于 2012-07-18T12:22:58.763 回答
1

添加以下代码:

@Override
    protected void onDestroy() {
        thread.stop();
        thread.destroy();
        super.onDestroy();
    }
于 2012-07-18T08:07:22.880 回答
1

Threadstop 在退出run方法时运行。所以,如果你有一个像

public void run() {
  while(condition) {
    // exec
  }
}

请记住设置conditionfalse,以便让线程完成其执行

class DBThread extends Thread {

private boolean finish = true;    

public void stopThread() {
   finish = false;
}

@Override
public void run() {
    while (finish) {
        Cursor cursor = null;

        Cursor mCursor = lrDB.selectUserDetails(mUser_Id);
        if (mCursor.getCount() > 0) {
            mCursor.moveToNext();
            String Name = mCursor .getString(0);
            String userName = mCursor .getInt(1);
            String password = mCursor .getString(3);

        }
        mCursor.close();

    }
 }
}

内部onDrestroy()调用yourThreadInstance.stopThread();

于 2012-07-18T07:51:28.843 回答
0

您可以调用正在运行的线程的中断函数并在线程上验证您是否使用 isInterrupted() 函数中断并相应地继续。

于 2012-07-18T12:06:27.523 回答
0
@Override     
protected void onStop() {
         thread.stop();
           super.onDestroy();
 }


 @Override     
protected void onPouse() {
         thread.stop();
        super.onDestroy();
}
于 2012-07-18T08:18:51.493 回答
0

您可以在 onStop 或 onDestroy 方法中处理这种情况。在 onDestroy 方法中中断线程。

于 2012-07-18T07:50:43.003 回答