Updating TextView from Async Task which use custom program dialog

前端 未结 4 1671
天命终不由人
天命终不由人 2020-12-10 08:29

In one of my app, I have a scenario where I need to do some background task. For doing that I am using Async Task. Also I am using custom progress dialog. Below is the layou

4条回答
  •  不思量自难忘°
    2020-12-10 08:43

    Yes, because you are trying to set the TextView inside the doInBackground() method, and this is not allowed,

    Why not allowed? Because There is a only one Thread running which is UI Main Thread, and it doesn't allowed to update UI from thread process. read more info here: Painless Threading

    So there is a solution if you want to set the TextView inside the doInBackground() method, do the UI updating operations inside the runOnUiThread method.

    Otherwise, suggestion is to do all the UI display/update related operations inside the onPostExecute() method instead of doInBackground() method of your AsyncTask class.

提交回复
热议问题