我已经尝试解决这个问题大约一周了,但我在这里查看了很多问题和答案,但我无法弄清楚:
我有一个自定义 View 类(下面的 DoodleView),允许用户用手指在屏幕上绘图。只要我不调用添加到 View 中的任何方法来允许更改参数(绘画颜色、画笔大小等), View 就可以正常工作。我相信我正确调用了方法,并且对象已初始化。我在这里缺少什么?
DoodleView代码(自定义 View 类):
package fibrs.coloringbook;
import android.R.bool;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class DoodleView extends View implements DoodleInterface {
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
public Paint mPaint;
Context context;
//private boolean first;
public DoodleView(Context c) {
super(c);
context=c;
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFFFF0000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(20);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
// This is one of the methods that throws nullpointerexception everytime
public void setColor(int color){
mPaint.setColor(color);
}
private void touch_start(float x, float y) {
//showDialog();
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.reset();
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SCREEN));
//mPaint.setMaskFilter(null);
}
@Override
public boolean onTouchEvent(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;
}
}
Activity :
package fibrs.coloringbook;
//Imports
public class ColorTimeActivity extends Activity {
private ImageView mImageView3;
String filePath;
Bitmap bitmapImage = null;
DoodleView mDV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_color_time);
Log.v(getString(R.string.app_name), "Layout set (and inflated?).");
mDV = new DoodleView(this);
mDV = (DoodleView) findViewById(R.id.canvas);
mImageView3 = (ImageView) findViewById(R.id.imageView3);
getPictureIntent(1);
};
// Other items omitted
public void colorChoser(int which){
Toast toast = Toast.makeText(this, Integer.toString(which), Toast.LENGTH_SHORT);
toast.show();
int color = 0xFFFFFF;
switch (which){
case 1: {
color = 0xFFFF0000; //red
} break;
case 2: {
color = 0xFFFFA500; //orange
} break;
case 3: {
color = 0xFFFFFF00; //yellow
} break;
case 4: {
color = 0xFF00FF00; //green
} break;
case 5: {
color = 0xFF0000FF; //blue
} break;
case 6: {
color = 0xFF800080; //purple
} break;
case 7: {
color = 0xFF000000; //black
} break;
case 8: {
color = 0xFFFFFFFF; //white
} break;
}
TextView textElement = (TextView) findViewById(R.id.color_button);
textElement.setTextColor(color);
mDV.setColor(color);
//mDV.mPaint.setColor(color); //This also causes a nullpointerexception
}
预先感谢您的所有帮助。如果格式不正确,我深表歉意。
最佳答案
请发布异常堆栈跟踪。
但是,在这里:
mDV = new DoodleView(this);
mDV = (DoodleView) findViewById(R.id.canvas);
您正在创建 DoodleView
,将引用放入 mDV
中,然后将引用替换为 findViewById()
返回的内容。这可能是null
。尝试在 null
上调用方法将导致 NPE。
相反,可以将 DoodleView
添加到 XML 中的 activity_color_time
布局,或者在代码中调用 addView()
将其作为子项附加到布局中的一些 View 组。
关于java - 我添加到自定义 Android View 的所有方法都会导致 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20634619/