1

这是在扩展 View 的 GraphView.java 中。我希望当这个 invalidate(bounds) 被调用时,它应该同时调用 onDraw() .. 这段代码中有什么错误。它给了我这个例外。

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

有什么帮助吗?请!!!

4

2 回答 2

1

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

于 2013-08-03T14:44:11.653 回答
0

尝试做:view.invalidate();因为它调用onDraw(Canvas canvas).

于 2013-08-03T14:27:57.080 回答