定时器/ TimerTask错误

时间:2013-07-30 18:56:25

标签: android timer timertask

我刚刚开始编写Android应用程序,现在我遇到了计时器和计时器任务的问题。

在我的应用程序中,我使用计时器来更新UI,但是当我开始测试时会出现错误,我必须强制关闭它。

这是我的计时器代码:

int delay = 5000; // delay for 5 sec.
int period = 1000; // repeat every sec.

Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() 
{
    public void run() 
    {
        moveLettersInCircle();

    }

}, delay, period);

Logcat说:

07-30 18:40:12.635: D/dalvikvm(334): GC_EXTERNAL_ALLOC freed 61K, 52% free 2599K/5379K, external 1625K/2137K, paused 52ms
07-30 18:40:16.186: D/dalvikvm(334): GC_EXTERNAL_ALLOC freed 36K, 51% free 2672K/5379K, external 3792K/4025K, paused 40ms
07-30 18:40:21.503: W/dalvikvm(334): threadid=9: thread exiting with uncaught exception (group=0x40015560)
07-30 18:40:21.526: E/AndroidRuntime(334): FATAL EXCEPTION: Timer-0
07-30 18:40:21.526: E/AndroidRuntime(334): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
07-30 18:40:21.526: E/AndroidRuntime(334):  at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
07-30 18:40:21.526: E/AndroidRuntime(334):  at android.view.View.requestLayout(View.java:8267)
07-30 18:40:21.526: E/AndroidRuntime(334):  at android.view.View.requestLayout(View.java:8267)
07-30 18:40:21.526: E/AndroidRuntime(334):  at android.view.View.requestLayout(View.java:8267)
07-30 18:40:21.526: E/AndroidRuntime(334):  at android.view.View.requestLayout(View.java:8267)
07-30 18:40:21.526: E/AndroidRuntime(334):  at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:257)
07-30 18:40:21.526: E/AndroidRuntime(334):  at android.view.View.requestLayout(View.java:8267)
07-30 18:40:21.526: E/AndroidRuntime(334):  at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:257)
07-30 18:40:21.526: E/AndroidRuntime(334):  at android.view.View.requestLayout(View.java:8267)
07-30 18:40:21.526: E/AndroidRuntime(334):  at android.view.View.setLayoutParams(View.java:5000)
07-30 18:40:21.526: E/AndroidRuntime(334):  at com.jest.womix.Game$1.run(Game.java:104)
07-30 18:40:21.526: E/AndroidRuntime(334):  at java.util.Timer$TimerImpl.run(Timer.java:284)
07-30 18:40:25.666: I/Process(334): Sending signal. PID: 334 SIG: 9

我该如何解决这个问题?

编辑HANDLER代码:

    Handler m_handler;
    Runnable m_handlerTask;  
    m_handler = new Handler();   
    m_handlerTask = new Runnable()
    {
      @Override 
      public void run() { 

        moveLettersInCircle(); 
        m_handler.postDelayed(m_handlerTask, 1000);  //Problem: The local variable m_handlerTask may not have been initialized --> But if I initialize it then I get another error in the line m_handlerTask = new Runnable()

      }
      };
     m_handlerTask.run();  

祝你好运

1 个答案:

答案 0 :(得分:4)

我猜你是从backgroudn线程更新ui。计时器在不同的线程上运行。

你应该在ui线程上更新ui。使用runOnUiThread

  runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                       // update ui here.
                    }
                });
  

android.view.ViewRoot $ CalledFromWrongThreadException:只有   创建视图层次结构的原始线程可以触及其视图。

以上一般意味着你是从另一个线程更新/访问ui。

您还可以使用Handler

Handler m_handler;
Runnable m_handlerTask ;  
m_handler = new Handler();   
m_handlerTask = new Runnable()
{
  @Override 
  public void run() { 

    // do something  
    m_handler.postDelayed(m_handlerTask, 1000);    

  }
  };
 m_handlerTask.run();   

取消运行m_handler.removeCallbacks(m_handlerTask)

编辑:

     timer.scheduleAtFixedRate( new TimerTask() {
            @Override
            public void run() {

               runOnUiThread(new Runnable() //run on ui thread
                 {
                  public void run() 
                  { 
                    // update ui here
                  }
                  });
            }
        }, 1000, 1000 );