gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:14:43 24 4
gpt4 key购买 nike

我正在尝试在 Android 应用程序中加载要在其上绘制的图片,但似乎无法加载要在其上绘制的图片。

activity_main.xml 的错误日志告诉我我的自定义 View 类中的 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;
}

}

我的自定义 View 类:

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;
}
}

最佳答案

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

@Override
protected void onDraw(Canvas canvas) {

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

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

你需要初始化这个变量。

关于java - onDraw() 方法中的 NullPointerException Android 绘图应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21746658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com