rmaik
rmaik

Reputation: 1086

Error when executing an asynchTask

i have a viewpager with some actiontabs each action tab has its own layout. in one of the layouts i have a button that when clicked an AsyncTask that changes the background color should be executed as shown below.

At run time I receive the below errors from the logcat.

please have a look at the logcat errors and kindly let me know what i am missing in the code.

Code

class AsyncColor extends AsyncTask<String, Integer, String> {

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        for (int i = 0; i < params.length; i++){
            rl_3.setBackgroundColor(Color.parseColor(params[i]));
        }
        return "done";
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        btn_asynch.setText(result);
    }
}

@Override
public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

    btn_asynch.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            new AsynchColor().execute(asynchBLUE, asynchGREEN, asynchRED, asynchBLUE1, asynchGREEN1, asynchRED1);
        }
    });

Logcat:

12-06 13:13:15.049: E/AndroidRuntime(5591): FATAL EXCEPTION: AsyncTask #1
12-06 13:13:15.049: E/AndroidRuntime(5591): java.lang.RuntimeException: An error occured while executing doInBackground()
12-06 13:13:15.049: E/AndroidRuntime(5591):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
12-06 13:13:15.049: E/AndroidRuntime(5591):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
12-06 13:13:15.049: E/AndroidRuntime(5591):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
12-06 13:13:15.049: E/AndroidRuntime(5591):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
12-06 13:13:15.049: E/AndroidRuntime(5591):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-06 13:13:15.049: E/AndroidRuntime(5591):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-06 13:13:15.049: E/AndroidRuntime(5591):     at java.lang.Thread.run(Thread.java:841)
12-06 13:13:15.049: E/AndroidRuntime(5591): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
12-06 13:13:15.049: E/AndroidRuntime(5591):     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6832)
12-06 13:13:15.049: E/AndroidRuntime(5591):     at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1053)
12-06 13:13:15.049: E/AndroidRuntime(5591):     at android.view.ViewGroup.invalidateChild(ViewGroup.java:4518)
12-06 13:13:15.049: E/AndroidRuntime(5591):     at android.view.View.invalidate(View.java:11687)
12-06 13:13:15.049: E/AndroidRuntime(5591):     at android.view.View.setBackgroundDrawable(View.java:16248)
12-06 13:13:15.049: E/AndroidRuntime(5591):     at android.view.View.setBackground(View.java:16139)
12-06 13:13:15.049: E/AndroidRuntime(5591):     at android.view.View.setBackgroundColor(View.java:16101)
12-06 13:13:15.049: E/AndroidRuntime(5591):     at com.example.viewpagerwithactiontabstest00.Aufgabe_7$AsynchColor.doInBackground(Aufgabe_7.java:72)
12-06 13:13:15.049: E/AndroidRuntime(5591):     at com.example.viewpagerwithactiontabstest00.Aufgabe_7$AsynchColor.doInBackground(Aufgabe_7.java:1)
12-06 13:13:15.049: E/AndroidRuntime(5591):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
12-06 13:13:15.049: E/AndroidRuntime(5591):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)

Upvotes: 0

Views: 42

Answers (2)

Mike
Mike

Reputation: 857

You are trying to Change a view on a Thread which is not the UIThread.

You will Need to move your

 for (int i = 0; i < params.length; i++){
        rl_3.setBackgroundColor(Color.parseColor(params[i]));
    }

into onPreExecute(runs onUIThread) or onPostExecute(Runs on Ui Thread) or a different logic with onProgressUpdate (which you can call with publishProgress();. But if your doInBackground is empty without that. I would simply recommend you to clear this asynctask and move the

 for (int i = 0; i < params.length; i++){
        rl_3.setBackgroundColor(Color.parseColor(params[i]));
    }

into a new method in your activity. I can't say if it is better due to performance to do such a small process directly on a UIThread, or call the onProgressUpdate several times. I think the difference must be really small, and you are saving a lot of code-lines which makes you code better looking..

Read more about AsyncTask: http://developer.android.com/reference/android/os/AsyncTask.html

And a tutorial about painless Threading: http://android-developers.blogspot.co.at/2009/05/painless-threading.html

Upvotes: 0

inni
inni

Reputation: 484

You cant touch UI thread elements In doInBackground

@Override 
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub 
        for (int i = 0; i < params.length; i++){
            publishProgress(params[i]);
        } 
        return "done"; 
    } 

    @Override 
    protected void onProgressUpdate(Integer... values) {
        // TODO Auto-generated method stub 
        super.onProgressUpdate(values);
        rl_3.setBackgroundColor(Color.parseColor(values[i]));
    } 

Upvotes: 1

Related Questions