执行asyncTask时出错

时间:2014-12-06 12:17:35

标签: android android-asynctask

我有一个viewpager,其中一些actiontabs每个操作标签都有自己的布局。在其中一个布局中,我有一个按钮,当点击AsyncTask时会改变背景 颜色应该如下所示执行。

在运行时,我从logcat收到以下错误。

请查看logcat错误,并请告诉我代码中缺少的内容。

代码

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)

2 个答案:

答案 0 :(得分:1)

您无法触摸doInBackground中的UI线程元素

@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]));
    } 

答案 1 :(得分:0)

您正在尝试更改不是UIThread的线程上的视图。

您需要移动

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

进入onPreExecute(runs onUIThread)onPostExecute(Runs on Ui Thread)或使用onProgressUpdate的不同逻辑(您可以使用publishProgress();进行调用。但是如果您的doInBackground没有它,则为空。我只是建议你清除这个asynctask并移动

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

进入您活动的新方法。我不能说由于性能直接在UIThread上做这么小的过程,或者多次调用onProgressUpdate。我认为差异必须非常小,而且你节省了很多代码行,这使你的代码看起来更好..

详细了解AsyncTask:http://developer.android.com/reference/android/os/AsyncTask.html

关于无痛线程的教程:http://android-developers.blogspot.co.at/2009/05/painless-threading.html