AsyncTask get() 方法无法正常工作 [英] AsyncTask get() method not working properly

查看:32
本文介绍了AsyncTask get() 方法无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码:

//(...)
translationTextView.setText("Searching for translation...");
translationTextView.setVisibility(View.VISIBLE);

myAsyncTask = (MyAsyncTask) new MyAsyncTask().execute(someString);

try {
    //As I understand it should wait here until AsyncTask is completed. But why for the time of execution translateTextView value is ""?
    translationTextView.setText(translateTask.get() + "<BR>");
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (ExecutionException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

问题是 translationTextView 的值是 "",直到 myAsyncTask 完成.所以它看起来像

The problem is that translationTextView value is "" until myAsyncTask is finished. So it looks like

translationTextView.setText("Searching for translation...");

没有被调用.我做错了什么?

is not called. What did I do wrong?

推荐答案

它被调用但是你调用

translationTextView.setText(translateTask.get() + "<BR>");

get() 是一个阻塞调用

and get() is a blocking call

尝试在您的 onPostExecute() 中设置文本.如果我理解正确,那么这样的事情应该给你你想要的

Try instead to set the text in your onPostExecute(). If I understand you correctly then something like this should give you what you want

   //(...)
        translationTextView.setText("Searching for translation...");
        translationTextView.setVisibility(View.VISIBLE);

        myAsyncTask = (MyAsyncTask) new MyAsyncTask().execute(someString);

然后,假设 MyAsyncTask 是您的 Activity 调用的内部类 translationTextView.setText() 插入您试图从 <代码>doInBackground()

Then, assuming MyAsyncTask is an inner class of your Activity call translationTextView.setText() inserting whatever you are trying to return from doInBackground()

这篇关于AsyncTask get() 方法无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆