我正在使用以下代码:

//(...)
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的值为“”。所以看起来像
translationTextView.setText("Searching for translation...");

不被调用。我做错什么了?

最佳答案

它被称为,但随后您调用

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

get()是一个阻止调用

尝试改为在onPostExecute()中设置文本。如果我正确理解您的意思,那么类似的东西应该可以给您您想要的东西
   //(...)
        translationTextView.setText("Searching for translation...");
        translationTextView.setVisibility(View.VISIBLE);

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

然后,假设MyAsyncTaskActivity调用的内部类translationTextView.setText()插入您要从doInBackground()返回的所有内容

09-26 21:26