我想以动态方式更改布局背景。
以下是LogCat的日志:
04-10 14:59:55.291: WARN/dalvikvm(263): threadid=15: thread exiting with uncaught exception (group=0x4001b188)
04-10 14:59:55.309: ERROR/AndroidRuntime(263): Uncaught handler: thread Thread-8 exiting due to uncaught exception
04-10 14:59:55.331: ERROR/AndroidRuntime(263): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
04-10 14:59:55.331: ERROR/AndroidRuntime(263): at android.view.ViewRoot.checkThread(ViewRoot.java:2683)
04-10 14:59:55.331: ERROR/AndroidRuntime(263): at android.view.ViewRoot.requestLayout(ViewRoot.java:557)
04-10 14:59:55.331: ERROR/AndroidRuntime(263): at android.view.View.requestLayout(View.java:7918)
04-10 14:59:55.331: ERROR/AndroidRuntime(263): at android.view.View.requestLayout(View.java:7918)
04-10 14:59:55.331: ERROR/AndroidRuntime(263): at android.view.View.requestLayout(View.java:7918)
04-10 14:59:55.331: ERROR/AndroidRuntime(263): at android.view.View.requestLayout(View.java:7918)
04-10 14:59:55.331: ERROR/AndroidRuntime(263): at android.view.View.setBackgroundDrawable(View.java:7275)
04-10 14:59:55.331: ERROR/AndroidRuntime(263): at android.view.View.setBackgroundResource(View.java:7188)
04-10 14:59:55.331: ERROR/AndroidRuntime(263): at walk.me.Splasher$1.run(Splasher.java:35)
walk.me.Splasher$1.run(Splasher.java:35) is this line:
layout.setBackgroundResource(temp); What should I do?
答案 0 :(得分:3)
使用处理程序或在UI线程上运行它。 处理程序可以更好地完成此任务。
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
}
});
答案 1 :(得分:2)
您无法从后台线程修改用户界面。请参阅文章PainlessThreading。从代码的结构来看,我强烈建议您使用该文章中描述的AsyncTask。它允许您运行后台线程,该线程重复触发UI线程上的操作。一旦学习了如何使用AsyncTask类,这种事情比将单个Runnable对象提供给Handler更容易。