Android类函数空对象引用

时间:2017-11-21 04:13:40

标签: java android canvas

我正在写一个非常简单的Android绘图程序,为此我编写了一个类DoodleCanvas.java,其中包括(在最底部)一个通过填充白色来清除画布的函数。 /强>

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class DoodleCanvas  extends View {

    private Paint mPaint;   //Paint object for setting attr
    private Path mPath;     //Path object for tracing Paint over
    public Canvas canvas;   //Canvas object for calling Paint

    public DoodleCanvas(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(10);
        mPath = new Path();
    }

    //Draw mPaint over mPath
    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawPath(mPath, mPaint);
        super.onDraw(canvas);
    }

    //Create mPath from touch events
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                mPath.moveTo(event.getX(), event.getY());
                break;
            case MotionEvent.ACTION_MOVE:
                mPath.lineTo(event.getX(), event.getY());
                invalidate();
                break;
        }
        return true;
    }

    //Class function for filling canvas with white, effectively cleaning it
    public void clearcanvas(Canvas canvas) {
        canvas.drawColor(Color.WHITE);
    }
}

在我的MainActivity.java中,我设置了一个清除按钮以致电clearcanvas

package brianslho.basiccanvas;

import android.graphics.Canvas;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    DoodleCanvas dcanvas; //Custom class DoodleCanvas (from "DoodleCanvas.java")
    Button btn;           //Button for clearing screen

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dcanvas = findViewById(R.id.canvas);
        btn = findViewById(R.id.btn);
        btn.setOnClickListener(clearcanvas);
    }

    //Clear screen on button click
    protected Button.OnClickListener clearcanvas = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //ERROR: canvas is null
            Canvas canvas = dcanvas.canvas;
            dcanvas.clearcanvas(canvas);
        }
    };
}

每当我按下清除按钮时,应用程序都会崩溃。实质上,错误消息是java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.Canvas.drawColor(int)' on a null object reference。我已经在onClick内声明了画布,我非常确定它不是“良好编码”,而只是尝试修复null问题。我已经搜索了StackOverflow类似的问题(男孩有很多),但找不到任何可以解决这个问题的东西。

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您的问题是您声明的变量具有相同的名称:public Canvas canvas; vs public void clearcanvas(Canvas canvas) =>程序将理解函数中的canvas是全局而不是函数的参数。 =>解决方案:更改变量名称

答案 1 :(得分:0)

你必须调用构造函数,并且你已声明了同名public Canvas canvas的变量;和public void clearcanvas(画布画布)。所以改变变量。

 dcanvas = new DoodleCanvas(Context context, AttributeSet attrs);