无效()不是从活动中发生的计时器 [英] Invalidate() is not happening from Activity in Timer

查看:282
本文介绍了无效()不是从活动中发生的计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对我的活动code无效它不是无效的画布。指的onDraw()不获取调用甚至一度;

This is my code on Activity to Invalidate the canvas it is not invalidating. Means onDraw() is not getting called even once;

   public GraphView  view;
    @Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);

          view  = GraphView(this,null);
            runplotTimer();
  } 


      public void  runplotTimer()
    {
    Timer t = new Timer();
    //Set the schedule function and rate
    t.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            InvalidateTimer();
        }      
    },1000,40); 
}

  public void InvalidateTimer()
{
     this.runOnUiThread(new Runnable() {
            @Override
            public void run()
            {
                 //Log.d(ALARM_SERVICE, "Timer of 40 miliseconds");
                  view.InvalidateGraph();
            } 
        });
 }

在View类,这是方法,是从流汗所谓的活动。根据需要与其他的OnDraw声明是一样的。

on View class this is method which is gettting called from Activity. other OnDraw declaration is same as required.

   public void InvalidateGraph()
  {
     m_bCalledPlotRealTimeGraph = true;
         invalidate(chanX_count1, 0, chanX_count1+7, graphheight);


  }   

任何帮助吗?

推荐答案

您正试图作出一个更改查看定时发,这是行不通的。你需要调用无效主(UI)线程:

You are attempting to make changes to the View on a Timer Thread, which will not work. You need to call invalidate on the main (UI) thread:

((Activity) view.getContext()).runOnUiThread(new Runnable() {
    @Override
    public void run() {
        invalidate(chanX_count1, 0, chanX_count1+7, graphheight);
    }
});

这篇关于无效()不是从活动中发生的计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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