2017-12-07 3 views
-1

그래서 사용자가 화면에서 그릴 수있는 간단한 그리기보기를 만들려고합니다. 그러나 몇 시간 동안 연구 한 후에도 여전히 onClick() 메서드에서 canvas.drawPath()를 호출하면 응용 프로그램이 중단되는 이유를 해결하는 데 문제가있는 것 같습니다. (내가 여기에 터치 방식을 생략 한)null null 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); 

} 

내 로그 캣이 오류 메시지가 표시됩니다 : 당신의 시간

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) 
... 

감사합니다!

+0

@csm_dev isSimplePath와 일치하는 코드가 없습니다. 위에 제공된 모든 코드는 isSimplePath에 대한 참조가없는 터치 메소드를 제외한 모든 View 클래스입니다. – Raddish18

답변

0

세 번째 생성자에서는 경로 및 페인트 객체 만 생성합니다. 두 개의 다른 생성자는 경로 및 페인트 변수를 초기화하지 않습니다. 사용되는 생성자에 따라 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); 

을 추가합니다.