gpt4 book ai didi

Android onDraw() 没有在 Timer 上被调用

转载 作者:行者123 更新时间:2023-11-30 03:27:22 24 4
gpt4 key购买 nike

这是在扩展 View 的 GraphView.java 中。我希望当调用此 invalidate(bounds) 时,它应该同时调用 onDraw().. 这段代码中的错误是什么。它给了我这个异常(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);
}

有什么帮助吗?请!!!

最佳答案

尝试:

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.

对比

Invalidate:

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

Source

关于Android onDraw() 没有在 Timer 上被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18033806/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com