首先对不起我的英语,我是西班牙语,现在,我在制作扩展 View 的自定义类时遇到问题。Theese 是我的课程和我的 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@android:color/white" >
<ImageButton
android:id="@+id/paleta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/paleta" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pizarra"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
public class Pintar extends View {
private Bitmap mBitmap = null;
private Canvas mCanvas = null;
private Path mPath = null;
private float mX, mY;
private static final float TOLERANCE = 4;
private LinearLayout pizarra;
public Pintar(Context context) {
super(context);
// obtener pantalla
pizarra= (LinearLayout) findViewById (R.id.pizarra);
mBitmap = Bitmap.createBitmap(pizarra.getWidth(),
pizarra.getHeight(), Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touchStart(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touchMove(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touchUp();
invalidate();
break;
}
return true;
}
private void touchStart(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touchMove(float x, float y) {
if (Math.abs(x - mX) >= TOLERANCE || Math.abs(y - mY) >= TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
}
}
private void touchUp() {
mPath.lineTo(mX, mY);
mCanvas.drawPath(mPath, Blackboard.mPaint);
mPath.reset();
}
@Override
protected void onDraw(Canvas canvas) {
// fondo
canvas.drawColor(0XFFBBBBBB);
// lo ya pintado
canvas.drawBitmap(mBitmap, 0, 0, null);
// el trazo actual
canvas.drawPath(mPath, Blackboard.mPaint);
}
public class Blackboard extends Activity {
public static Paint mPaint = null;
protected Pintar board;
private LinearLayout pizarra;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
board = new Pintar(this);
pizarra= (LinearLayout) findViewById (R.id.pizarra);
pizarra.addView(board);
setContentView(R.layout.main);
// preparamos el pincel
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0XFF00E1FF);
mPaint.setStrokeWidth(10);
}
我想在顶部的 LinearLayout 中显示一个 ImageButton,在底部的 LinearLayout 中我想用我的自定义类进行绘制,但我有这个错误:
06-25 18:43:07.578: E/AndroidRuntime(19822): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.acme.blackboard/com.acme.blackboard.Blackboard}: java.lang.NullPointerException
感谢您的回答