我正在为android开发一个简单的绘图应用程序。这个应用程序的结构很简单 - 它有一个包含文本的MainActivity类,以及一个膨胀第二个类的按钮,一个包含图形的自定义视图。
主类的代码如下:
public class MainActivity extends AppCompatActivity {
DrawingView dv;
ViewGroup parent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
parent = (ViewGroup) findViewById(R.id.activity_main);
}
public void crossClass(View v) {
//setContentView(R.layout.sample_drawing_view);
dv = new DrawingView(getApplicationContext());
View viw = getLayoutInflater().inflate(R.layout.sample_drawing_view, parent);
System.out.println("Attempted to Inflate DV");
}
public void refreshCallback(View v){
dv.refresh();
}
}
该类最重要的部分是创建了一个DrawingView实例,以便稍后在按下按钮时可以调用类中的方法。
方法(dv.refresh())很简单。
public void refresh(){
System.out.println("Refresh Command Sent");
drawCanvas.drawColor(Color.WHITE);
invalidate();
}
所有这一切都是在画布上画出白色。
这给了我错误:
java.lang.NullPointerException: Attempt to invoke virtual method void android.graphics.Canvas.drawColor(int) on a null object reference
我使用调试器进行了一些检查,发现在调用该方法时,Canvas和Bitmap变量为空,Picture Here.。
但是,当我将断点设置为创建类之后,变量不为空,Picture Here。
我将把整个Drawing View类代码放在下面,但是我看不出是什么导致它们变成null。
public class DrawingView extends View
{
//Constant
public int Colour = 0xFF000000;
public int width;
public int height;
//Drawing Var
public Canvas drawCanvas;
public Bitmap canvasBitmap;
public Path drawPath;
public Paint drawPaint;
public Paint canvasPaint;
public DrawingView(Context context){
super(context);
get_dim(context);
setupDrawing();
setupUI();
}
public DrawingView(Context context, AttributeSet attributeSet){
super(context, attributeSet);
setupDrawing();
setupUI();
}
public void get_dim(Context context){
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
}
public void setupUI() {
}
public void setupDrawing() {
drawPath = new Path();
drawPaint = new Paint();
drawPaint.setColor(Colour);
drawPaint.setAntiAlias(true);
drawPaint.setStrokeWidth(20);
drawPaint.setStyle(Paint.Style.STROKE);
drawPaint.setStrokeJoin(Paint.Join.ROUND);
drawPaint.setStrokeCap(Paint.Cap.ROUND);
canvasPaint = new Paint(Paint.DITHER_FLAG);
}
@Override
protected void onDraw(Canvas canvas){
canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
canvas.drawPath(drawPath, drawPaint);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh){
super.onSizeChanged(w, h, oldw, oldh);
System.out.println("ON SIZE CHANGED CALLED");
canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
drawCanvas = new Canvas(canvasBitmap);
}
public void handelAction(int action, int x, int y){
switch(action) {
case 0:
drawPath.moveTo(x, y);
break;
case 2:
drawPath.lineTo(x, y);
break;
case 1:
drawCanvas.drawPath(drawPath, drawPaint);
break;
}
{
invalidate();
}
}
@Override
public boolean onTouchEvent(MotionEvent event){
float x = event.getX();
float y = event.getY();
System.out.println(event.getAction());
handelAction(event.getAction(), (int) x, (int) y);
return true;
}
public void refresh(){
System.out.println("Refresh Command Sent");
drawCanvas.drawColor(Color.WHITE);
invalidate();
}
}