指向canvas.drawPath()的null指针

时间:2017-12-07 17:23:01

标签: android canvas nullpointerexception android-custom-view

所以我只是想创建一个简单的绘图视图,用户可以在屏幕上绘图。然而经过几个小时的研究后,我仍然无法解决原因,当我在onDraw()方法中调用canvas.drawPath()时,应用程序崩溃了。 (我在这里省略了触摸方法)

public class DrawTing extends View{

private Bitmap bm;
private Canvas canv;
private Path path;
Context context;
private Paint paint1;
private Paint paint;

public DrawTing(Context c) {
    super(c);
    this.context = c;
}

public DrawTing(Context c, AttributeSet attrs) {
    super(c, attrs);
    this.context = c;
}

public DrawTing(Context c, AttributeSet attrs, int defStyle) {
    super(c, attrs, defStyle);
    this.context = c;

    path = new Path();

    paint = new Paint();
    paint.setDither(true);
    paint.setAntiAlias(true);
    paint.setColor(Color.GREEN);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(10f);

}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);

    bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    canv = new Canvas(bm);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawPath(path, paint);      //NPE here

    //Things I have tried along with canvas.drawPath()

    //canvas = this.canv
    //canvas = canv;
    //canvas = new Canvas();
    //canvas.drawRect(0,0, getWidth(), getHeight(), paint);
    //canvas.drawColor(0xFFAAAAAA);
    //canvas.drawBitmap(bm,0,0, paint);

}

我的logcat显示此错误消息:

 FATAL EXCEPTION: main
                                                              Process: 
 com.ting.cian.ting, PID: 7989

 java.lang.NullPointerException: Attempt to read from field 'boolean 
 android.graphics.Path.isSimplePath' on a null object reference
                                                                  at 
 android.graphics.BaseCanvas.drawPath(BaseCanvas.java:295)
                                                                  at 
 android.graphics.Canvas.drawPath(Canvas.java:1652)
                                                                  at 
 com.ting.cian.ting.DrawTing.onDraw(DrawTing.java:124)
                                                                  at 
 android.view.View.draw(View.java:19119)
                                                                  at 
 android.view.View.updateDisplayListIfDirty(View.java:18069)
                                                                  at 
 android.view.View.draw(View.java:18847)
                                                                  at 
 android.view.ViewGroup.drawChild(ViewGroup.java:4214)
                                                                  at 
 android.view.ViewGroup.dispatchDraw(ViewGroup.java:4000)
                                                                  at 
 android.view.View.updateDisplayListIfDirty(View.java:18060)
                                                                  at 
 android.view.View.draw(View.java:18847)
 ...

谢谢你的时间!

1 个答案:

答案 0 :(得分:1)

您只在第三个构造函数中创建路径和绘制对象。另外两个构造函数不初始化路径和绘制变量。 根据使用的构造函数,这会导致NullPointerException。

要解决此问题,请添加

path = new Path();

paint = new Paint();
paint.setDither(true);
paint.setAntiAlias(true);
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(10f);

也是其他构造函数。