定时器/ TimerTask的错误 [英] Timer/TimerTask Error

查看:483
本文介绍了定时器/ TimerTask的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始的Andr​​oid应用程序编程及现在我有一个定时器和定时器任务出现了问题。

在我的应用我用一个定时器来更新UI,但是当我开始测试它得到一个错误,我必须强迫关闭它。

下面是我的定时器code:

  INT延迟= 5000; // 5秒的延迟。
INT周期= 1000; //重复每秒。定时器定时器=新的Timer();
timer.scheduleAtFixedRate(新的TimerTask()
{
    公共无效的run()
    {
        moveLettersInCircle();    }},延迟期);

LogCat中这样说:

  07-30 18:40:12.635:D / dalvikvm(334):GC_EXTERNAL_ALLOC释放61K,52%免费2599K / 5379K,1625K外部/ 2137K,暂停52ms
07-30 18:40:16.186:D / dalvikvm(334):GC_EXTERNAL_ALLOC释放36K,51%免费2672K / 5379K,3792K外部/ 4025K,暂停40毫秒
07-30 18:40:21.503:W / dalvikvm(334):主题ID = 9:螺纹未捕获的异常(组= 0x40015560)退出
07-30 18:40:21.526:E / AndroidRuntime(334):致命异常:定时器0
07-30 18:40:21.526:E / AndroidRuntime(334):$ android.view.ViewRoot CalledFromWrongThreadException:只有创建视图层次可以触摸其观点原来的线程。
07-30 18:40:21.526:E / AndroidRuntime(334):在android.view.ViewRoot.checkThread(ViewRoot.java:2932)
07-30 18:40:21.526:E / AndroidRuntime(334):在android.view.View.requestLayout(View.java:8267)
07-30 18:40:21.526:E / AndroidRuntime(334):在android.view.View.requestLayout(View.java:8267)
07-30 18:40:21.526:E / AndroidRuntime(334):在android.view.View.requestLayout(View.java:8267)
07-30 18:40:21.526:E / AndroidRuntime(334):在android.view.View.requestLayout(View.java:8267)
07-30 18:40:21.526:E / AndroidRuntime(334):在android.widget.RelativeLayout.requestLayout(RelativeLayout.java:257)
07-30 18:40:21.526:E / AndroidRuntime(334):在android.view.View.requestLayout(View.java:8267)
07-30 18:40:21.526:E / AndroidRuntime(334):在android.widget.RelativeLayout.requestLayout(RelativeLayout.java:257)
07-30 18:40:21.526:E / AndroidRuntime(334):在android.view.View.requestLayout(View.java:8267)
07-30 18:40:21.526:E / AndroidRuntime(334):在android.view.View.setLayoutParams(View.java:5000)
07-30 18:40:21.526:E / AndroidRuntime(334):在com.jest.womix.Game $ 1.run(Game.java:104)
07-30 18:40:21.526:E / AndroidRuntime(334):在java.util.Timer中的$ TimerImpl.run(Timer.java:284)
07-30 18:40:25.666:I /流程(334):发送信号。 PID:334 SIG:9

我该如何解决呢?

编辑HANDLER code:

 处理程序m_handler;
    可运行m_handlerTask;
    m_handler =新的处理程序();
    m_handlerTask =新的Runnable()
    {
      @覆盖
      公共无效的run(){        moveLettersInCircle();
        m_handler.postDelayed(m_handlerTask,1000); //问题:局部变量m_handlerTask可能没有被初始化 - >但是,如果我将其初始化然后我得到了行m_handlerTask =新的Runnable其他错误()      }
      };
     m_handlerTask.run();

最好的问候


解决方案

我猜你是从backgroudn线程更新界面。定时器运行在不同的线程。

您应该更新UI线程UI。使用 runOnUiThread

  runOnUiThread(新的Runnable(){                    @覆盖
                    公共无效的run(){
                       //这里更新UI。
                    }
                });


  

android.view.ViewRoot $ CalledFromWrongThreadException:只有
  创建视图层次可以触摸其观点原来的线程。


以上通常意味着你updaint /从另一个线程acessing UI。

您也可以使用处理程序

 处理程序m_handler;
可运行m_handlerTask;
m_handler =新的处理程序();
m_handlerTask =新的Runnable()
{
  @覆盖
  公共无效的run(){    // 做一点事
    m_handler.postDelayed(m_handlerTask,1000);  }
  };
 m_handlerTask.run();

要取消运行 m_handler.removeCallbacks(m_handlerTask)

编辑:

  timer.scheduleAtFixedRate(新的TimerTask(){
            @覆盖
            公共无效的run(){               runOnUiThread(新的Runnable()//在UI线程上运行
                 {
                  公共无效的run()
                  {
                    //在这里更新用户界面
                  }
                  });
            }
        },1000,1000);

I just started with programming of android applications and right now I have a problem with timer and timer tasks.

In my app I use a timer to update the UI but when I start to test it gets an error and I have to force close it.

Here is my timer code:

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 says this:

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

How can I solve this ?

Edited HANDLER Code:

    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();  

Best regards

解决方案

I guess you are updating ui from the backgroudn thread. Timer runs on a different thread.

You should update ui on the ui thread. Use runOnUiThread

  runOnUiThread(new Runnable() {

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

android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

The above generally means you are updaint/acessing ui from another thread.

You can also use a 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();   

To cancel the run m_handler.removeCallbacks(m_handlerTask).

Edit:

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

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

这篇关于定时器/ TimerTask的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆