AsyncTask get() method not working properly

后端 未结 2 1958
青春惊慌失措
青春惊慌失措 2021-01-25 18:00

I am using the following code:

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

myAsyncTa         


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-25 18:59

    It is called but then you call

    translationTextView.setText(translateTask.get() + "
    ");

    and get() is a blocking call

    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);
    

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

提交回复
热议问题