我试图在Android中创建自定义视图。我的视图将绘制一些通过addTimelines()方法传递的时间轴。 Logcat说有一个执行此代码的NullPointerException,调试我注意到onDraw()方法调用时,ArrayList listaTl为空(其值为null)。
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
public class TimelineView extends View
{
private Paint p;
private ArrayList<TimelineGrafica> listaTl;
public TimelineView(Context context, AttributeSet attrs)
{
super(context, attrs);
setVerticalScrollBarEnabled(true);
setHorizontalScrollBarEnabled(true);
setMinimumHeight(400);
if(!isInEditMode())
{init();}
init();
}
public void addTimelines(ArrayList<TimelineGrafica> tls)
{
listaTl = tls;
}
private void init()
{
this.p = new Paint();
}
private void disegnaPunta(Canvas c, Paint p, TimelineGrafica t)
{
int[] x = new int[3];
int[] y = new int[3];
x[0] = t.getTotaleDurata();
y[0] = t.getCoordinataY() - 20;
x[1] = t.getTotaleDurata() + 40;
y[1] = t.getCoordinataY() + (t.getSpessoreTimeline()/2);
x[2] = t.getTotaleDurata();
y[2] = t.getCoordinataY() + t.getSpessoreTimeline() + 20;
c.drawLine(x[0], y[0], x[1], y[1], p);
c.drawLine(x[1], y[1], x[2], y[2], p);
c.drawLine(x[0], y[0], x[2], y[2], p);
}
private void disegnaRettangolo(Canvas c, Paint p, TimelineGrafica t)
{
Rect r = new Rect(t.getCoordinataX(),
t.getCoordinataY(),
t.getTotaleDurata(),
t.getCoordinataY() +
t.getSpessoreTimeline());
c.drawRect(r, p);
}
private void disegnaGrain(Canvas c, Paint p, TimelineGrafica t)
{
//Calcolo il 5% di distanza dalla punta
int numeroFatti = (t.getTotaleDurata() -
(t.getTotaleDurata()*3/100))/
t.getDistanzaIntervalli();
for(int i=0, cX = t.getCoordinataX(); i<numeroFatti;
i++, cX += t.getDistanzaIntervalli())
{
c.drawLine(cX, t.getCoordinataY() - 20, cX,
t.getCoordinataY() + t.getSpessoreTimeline() + 20, p);
if(i<=9)
c.drawText(""+i, cX-3, t.getCoordinataY() +
t.getSpessoreTimeline() + 35, p);
else
c.drawText(""+i, cX-5, t.getCoordinataY() +
t.getSpessoreTimeline() + 35, p);
}
}
@Override
public void onDraw(Canvas canvas)
{
if(!listaTl.isEmpty())
for(TimelineGrafica t : listaTl)
{
canvas.drawColor(Color.GRAY);
p.setColor(t.getColor());
p.setStyle(Paint.Style.FILL_AND_STROKE);
p.setAntiAlias(true);
p.setStrokeWidth(1);
disegnaGrain(canvas,p,t);
disegnaRettangolo(canvas,p,t);
disegnaPunta(canvas,p,t);
}
}
}
在这里,我调用方法添加一个新的时间轴来绘制。
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ArrayList<TimelineGrafica> tl = new ArrayList<TimelineGrafica>();
this.disegnoTimeline = new TimelineView(this, null);
PlainTimeLine tlp = ottieniTimeline("Timeline1.txt");
TimelineGrafica tlpp = new TimelineGrafica(tlp, 10, 30, 20, Color.WHITE);
tl.add(tlpp);
this.disegnoTimeline.addTimelines(tl);
setContentView(R.layout.timeline_view);
}
如果我在addTimelines()之后调用postInvalidate()/ invalidate()方法,问题就出现了。
有谁知道问题出在哪?
答案 0 :(得分:0)
方法postInvalidate()或invalidate(),当它被调用时,方法onDraw()将被调用 因此,当您更改TimelineView(如addTimelines(ArrayList))时,应使用postInvalidate()或invalidate()
刷新自身