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

后端 未结 4 2011
情歌与酒
情歌与酒 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
    0 讨论(0)
  • 2021-02-20 11:11

    If you want to show the progress during the background process, none of the previous answers give an hint: When OnPostExecute is called everything is finished so there's no need to updated status of the loading.

    So, you have to override onProgressUpdate in your AsyncTask and be sure that the second object is not a Void but an Integer or a String that is read by onProgressUpdate. For example using String:

    public class MyAsyncTask extends AsyncTask<Void, String, Void> {
    
        @Override
        protected void onProgressUpdate(String... values) {
            super.onProgressUpdate(values);
            if (values != null && values.length > 0) {
                //Your View attribute object in the activity
                // already initialized in the onCreate!
                mStatusMessageView.setText(values[0]);
            }
        }
    }
    

    Than in the background method you call publishProgress(value as String) which automatically calls onProgressUpdate method which use main UI thread:

    @Override
    protected Boolean doInBackground(Void... params) {
        your code...
        publishProgress("My % status...");
        your code...
        publishProgress("Done!");
    }
    

    More in general, when you declare an AsyncTask you have to specify the <Params, Progress, Result> classes that are passed to the main methods, these can be your own classes or just Java classes:

    • doInBackground(Params... params)
    • onProgressUpdate(Progress... value)
    • onPostExecute(Result... success)
    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2021-02-20 11:27

    You are trying to access UI components (View) from a background thread in your case inside the doInBackground() method. You are not allowed to do that.

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