我正在使用以下代码:
//(...)
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);
然后,假设 MyAsyncTask
是您的 Activity
调用 translationTextView.setText()
的内部类,插入您试图从 doInBackground()
关于java - AsyncTask get() 方法无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17204309/