0

所以我在片段中有一个线程'ThreadDemo',它进行一些计算并使用 getActivity().runOnUiThread() 来更新 TextView。它工作正常,但是当我退出应用程序时,它会在 getActivity() 上提供 NPE。根据我对android中线程的了解,它们应该在 Activity 被销毁时停止,但在这里,由于某种原因,它会继续并且在我关闭它时无法找到它。这是我的片段中的线程代码-

 public class ThreadDemo implements Runnable {


           int hour, minute, second, day=0;


           public void run() {
               while((196-day)>=0) {

                   Calendar cal = Calendar.getInstance();
                    day = cal.get(Calendar.DAY_OF_YEAR);
                    hour = cal.get(Calendar.HOUR_OF_DAY);
                    minute = cal.get(Calendar.MINUTE);
                    second = cal.get(Calendar.SECOND);

                    try {
**NPE here**             getActivity().runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                ....
                            }
                        });
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }                   
              }
           }


        } 

这是LogCat-

06-07 12:05:28.903: E/AndroidRuntime(19834): FATAL EXCEPTION: Thread-5858
06-07 12:05:28.903: E/AndroidRuntime(19834): java.lang.NullPointerException
06-07 12:05:28.903: E/AndroidRuntime(19834):    at com.example.mapsapiv2demo.Fragment3$ThreadDemo.run(Fragment3.java:59)
06-07 12:05:28.903: E/AndroidRuntime(19834):    at java.lang.Thread.run(Thread.java:856)

注意:我尝试在 onPause 方法中手动停止线程,但似乎 stop() 方法已被线程弃用,不推荐使用。知道我做错了什么或任何解决方法的建议吗?我知道类似 asynctask 的东西,但我是初学者,所以我现在想避免这种情况。谢谢你的帮助。

4

3 回答 3

0

停止线程并不像看起来那么简单。在这里阅读更多。我建议您在活动被销毁时在线程中设置一个标志,并检查该标志是否应该跳出线程中的循环。例子:

public class YourActivity extends Activity {

    private YourThread thread;

    @Override
    public void onStop() {
        super.onStop();
        thread.stopNow();
    }   
}

public class YourThread extends Thread {

    private boolean stopNow = false;

    public void stopNow() {
        stopNow = true;
    }

    public void run() {
        while(someCondition && !stopNow) {
            // do something
            if(!stopNow) {
                getActivity().runOnUiThread(...)
            }
        }
    }

}

您还可以查看切换到AsyncTask,在其上调用cancel()并在 doInBackground() 的循环中检查isCancelled ()

于 2013-06-07T07:01:04.970 回答
0

活动

 private Thread thread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        thread = new Thread(new ThreadDemo());
        thread.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        thread.interrupt();
    }

可运行

class ThreadDemo implements Runnable {


        int hour, minute, second, day = 0;


        public void run() {
            while ((196 - day) >= 0 && !Thread.currentThread().isInterrupted()) {

                Calendar cal = Calendar.getInstance();
                day = cal.get(Calendar.DAY_OF_YEAR);
                hour = cal.get(Calendar.HOUR_OF_DAY);
                minute = cal.get(Calendar.MINUTE);
                second = cal.get(Calendar.SECOND);

                try {
                    getActivity().runOnUiThread(new Runnable() {

                        @Override
                        public void run() {

                        }
                    });
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    Thread.currentThread().interrupt();
                }
            }
        }


    }
于 2013-06-07T07:35:26.187 回答
0

只需在循环内进行检查:

if(shouldStop) {
    break;
}
于 2013-06-07T06:55:02.203 回答