AsyncTask “Only the original thread that created a view hierarchy can touch its views.”

后端 未结 4 2042
情歌与酒
情歌与酒 2021-02-20 10:50

I try to modify the Spinner content across the AsyncTaks but I can\'t and the Logcat is wrote \"09-19 16:36:11.189: ERROR/ERROR THE(6078): Only the original thread that created

4条回答
  •  情话喂你
    2021-02-20 11:15

    As mentioned by Peter, you cannot access the views using doInBackground(). You can do that inside onPostExecute() however. That is where you are supposed to work with the results doInBackground() return as far as I know.

    I ran into this issue and fixed it by moving the view modification code to onPostExecute().

    For those of you who are starting to pick up Android development:
    - doInBackground(): whatever insides happen in another thread (in the background) different from the main/ original thread your views/ fragment/ activity operates on (this is the whole point of AsyncTask ==> you cannot touch the views!
    - onPostExecute(): now background stuffs are done, here it's back on the main/ thread thread ==> you can touch the views now!

提交回复
热议问题