Tejas
Tejas

Reputation: 443

Android onDraw() is not getting called on Timer

This is in GraphView.java which extends View. I want that when this invalidate(bounds) is getting called, at the same time it should call onDraw().. What is bug in this code. It is giving me this exception.

 01-01 00:45:42.813: E/AndroidRuntime(1586): FATAL EXCEPTION: Timer-2
 01-01 00:45:42.813: E/AndroidRuntime(1586): java.lang.NullPointerException
  01-01 00:45:42.813: E/AndroidRuntime(1586):     at          android.view.View.invalidate(View.java:8467)
  01-01 00:45:42.813: E/AndroidRuntime(1586):     at        com.cdl.mircam.GraphView$1.run(GraphView.java:327)
01-01 00:45:42.813: E/AndroidRuntime(1586):   at java.util.Timer$TimerImpl.run(Timer.java:284)
@Override
public void onDraw(Canvas c) 
    {
        super.onDraw(c);
        drawStuff(c);
    }


 public void drawStuff(Canvas canvas) 
   {    
   try
    {
        PlotRealTimeGraph(canvas);
        bounds = new Rect(chanX_count1+0, 0, chanX_count1+5, graphheight);
        canvas.drawRect(bounds,myPaint);
        }
   catch(Exception e1)
    {
    e1.printStackTrace();
    }
  }   


    public void InvalidatePlotRealTimeGraph()
     {
        Timer t = new Timer();
        t.scheduleAtFixedRate(new TimerTask() {
       @Override
        public void run() 
        {
           invalidate(bounds);
        }     
      },1000,40);   
    } 

Any help? please!!!

Upvotes: 1

Views: 644

Answers (2)

Abhishek Shukla
Abhishek Shukla

Reputation: 1242

Try doing: view.invalidate(); as it calls onDraw(Canvas canvas).

Upvotes: 0

Boris Mocialov
Boris Mocialov

Reputation: 3489

try:

if(bounds != null)
   postInvalidate (leftOfYourBoundsObject, topOfYourBoundsObject, rightOfYourBoundsObject, bottomOfYourBoundsObject);

PostInvalidate:

Cause an invalidate of the specified area to happen on a subsequent cycle through the event loop.

vs

Invalidate:

If the view is visible, onDraw(android.graphics.Canvas) will be called at some point in the future

Source

Upvotes: 1

Related Questions