2012-07-10 65 views
0

我尝试更改相对布局背景颜色,像blink首先设置绿色,然后设置相对布局中的白色背景颜色。但是,我的问题线程执行第二次强制关闭错误发生并尝试修复很多方式,但不起作用。 这里是我的代码:相对布局更改使用线程的背景颜色

 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) 

什么错在我的代码,以及如何解决这个问题?

在此先感谢!

回答

1

改变你的代码使用runOnUiThread从非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(); 
    } 
+0

将整个事物放在'runOnUiThread'里面是一个糟糕的主意,因为在这种情况下它没有任何意义。 – 2012-07-10 12:35:49

+0

然后你给我-1票。但你可以看看我已经放在里面的答案runOnUiThread – 2012-07-10 12:36:58

+0

如果有人要实现你的答案,他们最终会开始一个活动,开始一个单独的线程,它将线程的整个任务回到主线程。首先不需要单独的线程。 – 2012-07-10 12:42:11

1

您只能在主线程中更改视图元素。如果你在另一个线程中,使用这个:

Current_Activity.this.runOnUiThread(new Runnable() { 

       @Override 
       public void run() { 
        // TODO Auto-generated method stub 

       } 
      }); 
+0

我是尝试你的代码,但没有打开的应用程序总是空白我想要在后台运行线程和我开始在onCreate()方法的线程! – Dinesh 2012-07-10 12:42:29

1

你需要在GUI线程上执行与GUI相关的任务。

环绕你relative.setBackgroundColor(Color.GREEN);runOnUiThread像这样:

Current_Activity.this.runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      relative.setBackgroundColor(Color.GREEN);    
     } 
+0

我尝试你的代码,但没有打开应用程序总是空白我想要在后台运行线程,我开始线程在onCreate()方法! – Dinesh 2012-07-10 12:46:21