problem with a text view

前端 未结 2 608
长发绾君心
长发绾君心 2021-01-27 12:50

I\'m reading some data like speed and curent location using a single ton class....I\'m reading them using an AsyncTask thread in a loop....:)....each time I\'m reading a new spe

相关标签:
2条回答
  • 2021-01-27 13:47

    You should always post UI updates on the UIThread.

    runOnUiThread(new Runnable() {
            public void run() {
                 speed_1.setText(Integer.toString(speed));
                 location.setText(loc1);
    
            }
    }
    
    0 讨论(0)
  • 2021-01-27 13:56

    THelper is right, you cannot do any UI operations from a different thread than the main / UI one. You can either use his solution (which works great) or, since you are already using an AsyncTask you can use the methods provided by it:

    • publishProgress
    • onProgressUpdate

    Basically, from inside your doInBackground method you call publishProgress() and the AsyncTask class handles all thread-related headaches and calls your onProgressUpdate as soon as possible, on the UI thread, ensuring that you can modify the UI (for example call setText) without any problems.

    0 讨论(0)
提交回复
热议问题