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

后端 未结 4 2017
情歌与酒
情歌与酒 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:03

    A little more details

    Problem:

    • UI elements can be modified in the UI thread only. One exception is their postInvalidate() method (and its variants) that can be executed from outside the UI Thread
    • the error you receive is caused by an attempt to modify a UI element (a View) from outside the UI thread - doInBackground() runs on a background thread

    Solution:

    • modify the views in the onPostExecute() method - it runs on the UI thread
    • obtain a reference to the an Activity and use its runOnUiThread(Runnable) method
    • depending on the modification use one of the View's post...() methods

提交回复
热议问题