AsyncTask的不能在Android的线程工作

AsyncTask的不能在Android的线程工作

本文介绍了AsyncTask的不能在Android的线程工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的AsyncTask改变TextView的文字是这样的:

I use AsyncTask to change text of TextView like this:

    private class LongOperation extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        String response = "";
        for (String url : urls) {
            response += url;
        }
        return response;
    }

    @Override
    protected void onPostExecute(String result) {
        textView.setText(result);
    }
}

如果我把它在OnClick事件一切都将罚款:

Everything will fine if I call it in OnClick event:

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    textView = (TextView) findViewById(R.id.txt);
    Button button = (Button)this.findViewById(R.id.button);
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
          new LongOperation().execute(new String[]{"Hello"});
        }
      });
   }

但问题时,我在线程中调用它,程序强制关闭

But the problem when I called it in my thread, the program forced close

this.closeButton.setOnClickListener(new OnClickListener() {
         @Override
        public void onClick(View v) {
          Thread t= new Thread(){
           @Override
             public void run(){
              try{
                    //Do something
                    //Then call AsyncTask
                new LongOperation().execute(new String[]{"Hello"});

               }catch(Exception e){}
         }
        };
         t.start();
        }
      });

我在哪里错了?我不明白'电话差异螺纹或不是如何AsyncTask的。

Where am I wrong? I dont' understand how difference call AsyncTask in thread or not.

推荐答案

我建议你咨询和以更好地理解它是如何工作。从本质上讲,你应该在主线程创建子类。

I recommend you consult the AsyncTask documentation and Processes and Threads for a better understanding of how it works. Essentially, you should create your AsyncTask subclass on the main thread.

当你调用AsyncTask.execute(),您提供的,<一个href=\"http://developer.android.com/reference/android/os/AsyncTask.html#on$p$pExecute%28%29\">AsyncTask.on$p$pExecute被称为主线程,所以你可以做的UI设置。

When you call AsyncTask.execute(), your provided, AsyncTask.onPreExecute is called on the main thread, so you can do UI setup.

下一页AsyncTask.doInBackground方法被调用,并在其自己的线程中运行。

Next AsyncTask.doInBackground method is called, and runs in its own thread.

最后,当你的AsyncTask.doInBackground方法完成后,一个调用,以AsyncTask.onPostExecute在主线程上,你可以做任何UI清理工作。

Finally, when your AsyncTask.doInBackground method completes, a call is made to AsyncTask.onPostExecute on the main thread, and you can do any UI cleanup.

如果您需要更新来自内部的AsyncTask.doInBackground方法,调用AsyncTask.publishProgress,这将调用onProgressUpdate在主线程。

If you need to update the UI from within your AsyncTask.doInBackground method, call AsyncTask.publishProgress, which will invoke onProgressUpdate in the main thread.

这篇关于AsyncTask的不能在Android的线程工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 16:56