我有一個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)
所有UI更新必須位於onPostExecute,onPreExecute,onProgressUpdate。你不能在onBackground更新UI,請移動'rl_3.setBackgroundColor(Color.parseColor(params [i]));'以上功能 – 2014-12-06 12:19:54
你不能在doInBackground方法中進行UI更新 – Amy 2014-12-06 12:31:48