这是在GraphView.java中扩展View。我想在调用此无效(bounds)时同时调用onDraw()..这段代码中的bug是什么。它给了我这个例外。
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);
}
有任何帮助吗?请!
答案 0 :(得分:1)
尝试:
if(bounds != null)
postInvalidate (leftOfYourBoundsObject, topOfYourBoundsObject, rightOfYourBoundsObject, bottomOfYourBoundsObject);
PostInvalidate:
导致后续发生指定区域无效 循环通过事件循环。
VS
的Invalidate:
如果视图可见,将调用onDraw(android.graphics.Canvas) 在未来的某个时刻
答案 1 :(得分:0)
尝试做:view.invalidate();它调用onDraw(Canvas canvas)