Problem with Toast in AsyncTask method call

前端 未结 4 984
故里飘歌
故里飘歌 2020-12-10 08:54

Hey Everybody,
I have an AsyncTask that posts some data to a server. It does this by calling a static method that I wrote from doInBackground. When I run the AsyncTask,

4条回答
  •  离开以前
    2020-12-10 09:31

    The code in the doInBackground() method runs on its own thread so you cannot access any UI element from there directly as they are running on the UI thread.

    So you got two options.

    1. You handle all the UI stuff in the onPreExecute() and onPostExecute() method which run on the UI thread.

    2. You handle UI stuff in the onProgressUpdate() method which also runs on the UI thread. You can trigger this method from within doInBackground() by calling publishProgress().

提交回复
热议问题