Android 相对布局使用线程更改背景颜色
我正在尝试更改相对布局背景色,例如在相对布局中先闪烁设置绿色,然后设置白色背景色。但是,我的问题线程在发生第二次强制关闭错误时执行,并尝试修复很多方法,但都不起作用。 这是我的密码:Android 相对布局使用线程更改背景颜色,android,Android,我正在尝试更改相对布局背景色,例如在相对布局中先闪烁设置绿色,然后设置白色背景色。但是,我的问题线程在发生第二次强制关闭错误时执行,并尝试修复很多方法,但都不起作用。 这是我的密码: isThreadRunning = true; relative=(RelativeLayout) findViewById(R.id.layout); Runnable runnable = new Runnable() { public
isThreadRunning = true;
relative=(RelativeLayout) findViewById(R.id.layout);
Runnable runnable = new Runnable() {
public void run() {
int i=0;
while(isThreadRunning) {
if(i==0)
{
relative.setBackgroundColor(Color.GREEN);
i=1;
}
else
{
relative.setBackgroundColor(Color.BLACK);
i=0;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
new Thread(runnable).start();
我在logcat中得到如下错误:
07-10 17:48:42.752: W/dalvikvm(2060): threadid=9: thread exiting with uncaught exception (group=0x40015560)
07-10 17:48:42.756: E/AndroidRuntime(2060): FATAL EXCEPTION: Thread-10
07-10 17:48:42.756: E/AndroidRuntime(2060): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
07-10 17:48:42.756: E/AndroidRuntime(2060): at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
07-10 17:48:42.756: E/AndroidRuntime(2060): at android.view.ViewRoot.invalidateChild(ViewRoot.java:642)
07-10 17:48:42.756: E/AndroidRuntime(2060): at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:668)
07-10 17:48:42.756: E/AndroidRuntime(2060): at android.view.ViewGroup.invalidateChild(ViewGroup.java:2511)
07-10 17:48:42.756: E/AndroidRuntime(2060): at android.view.View.invalidate(View.java:5279)
07-10 17:48:42.756: E/AndroidRuntime(2060): at android.view.View.setBackgroundDrawable(View.java:7626)
07-10 17:48:42.756: E/AndroidRuntime(2060): at android.view.View.setBackgroundColor(View.java:7516)
07-10 17:48:42.756: E/AndroidRuntime(2060): at com.example.screen_blinker.Home_activity$1.run(Home_activity.java:44)
07-10 17:48:42.756: E/AndroidRuntime(2060): at java.lang.Thread.run(Thread.java:1019)
我的代码中有什么错误以及如何修复此问题
提前谢谢 只能在主线程中更改视图元素。如果您在另一个线程中,请使用以下命令:
Current_Activity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
}
});
您需要在GUI线程上执行与GUI相关的任务 包装你的
相对.setBackgroundColor(Color.GREEN)代码>在runOnUiThread
中,如下所示:
Current_Activity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
relative.setBackgroundColor(Color.GREEN);
}
将代码更改为用于从非UI线程更新UI元素:
public void myThread(){
Thread th=new Thread(){
@Override
public void run(){
try
{
while(isThreadRunning)
{
Thread.sleep(1000);
Current_Activity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
if(i==0)
{
relative.setBackgroundColor(Color.GREEN);
i=1;
}
else
{
relative.setBackgroundColor(Color.BLACK);
i=0;
}
}
});
}
}catch (InterruptedException e) {
// TODO: handle exception
}
}
};
th.start();
}
把整件事都放在runOnUiThread里面是个坏主意,因为在这种情况下,有一个单独的线程是没有意义的。然后你给我-1票。但是你可以看看我放在runOnUiThread里面的答案,如果有人要实现你的答案,它们将以一个活动结束,该活动启动一个单独的线程,该线程将该线程的整个任务重新排列在主线程上。首先不需要单独的线程。你的意思是相对的。setBackgroundColor(Color.BLACK);在非UI线程的部分中,您可以更新relative.setBackgroundColor(Color.BLACK);来自后台线程的行???是的,我想在后台线程上运行我正在尝试您的代码始终我的应用程序挂起未打开显示警报,如forceclose或wait!我正在尝试您的代码,但未打开应用程序始终为空我希望在后台运行线程,我是onCreate()方法中的启动线程!我正在尝试您的代码,但未打开应用程序始终为空我希望在后台运行线程,我是onCreate()方法中的启动线程!