0

我正在尝试制作一个列表视图,每行都将包含此 NotesSurface 自定义视图。我在c.drawCircle(25, 25, 25, null);上得到 NullPointer 异常;(c 不为空)。

public class NotesSurface extends SurfaceView implements View.OnTouchListener {

public NotesSurface(Context context) {
    super(context);
    init();
}

public NotesSurface(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public NotesSurface(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

private void init() {
    getHolder().addCallback(new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            onSurfaceCreated(holder);
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {

        }
    });
}

private void onSurfaceCreated(SurfaceHolder holder) {
    drawStaveLines();
}

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    return false;
}

private void drawStaveLines() {
    Canvas c = getHolder().lockCanvas();

    c.drawCircle(25, 25, 25, null);

    getHolder().unlockCanvasAndPost(c);
}

}

这是堆栈跟踪:

java.lang.NullPointerException
        at android.graphics.Canvas.drawCircle(Canvas.java:972)
        at tsikon.notes.editor.NotesSurface.drawStaveLines(NotesSurface.java:75)
        at tsikon.notes.editor.NotesSurface.onSurfaceCreated(NotesSurface.java:53)
        at tsikon.notes.editor.NotesSurface.access$000(NotesSurface.java:16)
        at tsikon.notes.editor.NotesSurface$1.surfaceCreated(NotesSurface.java:37)
        at android.view.SurfaceView.updateWindow(SurfaceView.java:533)
        at android.view.SurfaceView.access$000(SurfaceView.java:81)
        at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:169)
        at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:590)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1617)
        at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4424)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
        at dalvik.system.NativeStart.main(Native Method)

这是xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <view
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        class="tsikon.notes.editor.NotesSurface"
        android:id="@+id/editor_row_notesSurface"
        android:layout_gravity="left|center_vertical" />
</LinearLayout>

任何想法为什么会发生这种情况?谢谢。

4

2 回答 2

0

in the developer reference drawCircle it does not say that the paint can be null i think you have to specify an actual paint

于 2013-10-17T23:51:57.427 回答
0

您将Paint参数作为null. 这可以通过 Canvas 类中的一些方法来完成,但不能通过drawCircle(). 看这里:

http://developer.android.com/reference/android/graphics/Canvas.html#drawCircle(float,%20float,%20float,%20android.graphics.Paint)

于 2013-10-17T23:51:38.823 回答