最初は英語で申し訳ありませんが、スペイン語ですが、Viewを拡張するカスタムクラスの作成に問題があります。これらは私のクラスと私の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
答えてくれてありがとう