onDraw()方法中的NullPointerException Android绘图应用程序?

时间:2014-02-13 06:18:40

标签: java android nullpointerexception ondraw

我正在尝试将图片加载到Android应用程序的顶部,但似乎无法在上面加载绘画。

activity_main.xml的ErrorLog告诉我,我的自定义视图类中的onDraw()方法在canvas.drawBitmap(mBitmap,0,0,mBitmapPaint)行中遇到问题;`


activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <edu.berkeley.cs160.opalkale.prog2.DrawingView

        android:id="@+id/drawing"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_marginBottom="3dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="3dp"
        android:layout_weight="1"
        android:src="@drawable/crayon" />

</LinearLayout>

我的MainActivity.java文件:

package edu.berkeley.cs160.opalkale.prog2;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.Menu;


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.crayon);
    Canvas canvas = new Canvas (mBitmap);


    //DrawingView drawingView = (DrawingView)findViewById(R.id.drawing);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

我的自定义视图类:

import android.view.MotionEvent;
import android.view.View;

public class DrawingView extends View {
private Path mPath;
private Paint mPaint;

private Canvas mCanvas;
private Canvas shadowCanvas;

private Bitmap colorBitmap;
private Bitmap grayscaleBitmap;
private Bitmap mBitmap;
private Bitmap shadowBitmap;

private Paint mBitmapPaint;

private ArrayList<Path> paths = new ArrayList<Path>();

public DrawingView(Context context, AttributeSet attrs){
    super(context, attrs);

    mBitmapPaint = new Paint(Paint.DITHER_FLAG);

    mPath = new Path();

    mPaint = new Paint();
    mPaint.setColor(Color.GRAY);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeWidth(3);

    mCanvas = new Canvas ();

    paths.add(mPath);
}


@Override
protected void onDraw(Canvas canvas) {

canvas.drawBitmap(mBitmap,0,0,mBitmapPaint);

    for (Path p : paths) {
        canvas.drawPath(p, mPaint);
    }
}

private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;

private void touch_start(float x, float y) {
    mPath.reset();
    mPath.moveTo(x, y);
    mX = x;
    mY = y;
}

private void touch_move(float x, float y) {
    float dx = Math.abs(x - mX);
    float dy = Math.abs(y - mY);
    if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
        mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
        mX = x;
        mY = y;
    }
}

private void touch_up() {
    mPath.lineTo(mX, mY);
    // commit the path to our offscreen
    mCanvas.drawPath(mPath, mPaint);
    // kill this so we don't double draw
    mPath = new Path();
    paths.add(mPath);
}


public boolean onTouch(View arg0, MotionEvent event) {
    float x = event.getX();
    float y = event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            touch_start(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            touch_move(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_UP:
            touch_up();
            invalidate();
            break;
    }
    return true;
}
}

1 个答案:

答案 0 :(得分:2)

你有一个未初始化的变量mBitmap,这就是它抛出NPE的原因。

@Override
protected void onDraw(Canvas canvas) {

canvas.drawBitmap(mBitmap,0,0,mBitmapPaint);             // NPE here 

    for (Path p : paths) {
        canvas.drawPath(p, mPaint);                     
    }
}

您需要初始化此变量。