色の整数をパラメーターとして渡すことでボタンのクリック時に色を変更したいのですが、クリックするとAndroidがNull Pointer Exceptionのエラーをスローします。そしてアプリケーションがクラッシュします..私はここで立ち往生しています親切に助けてください関数名はSetColor()です
MyView Class
public class MyView extends Activity {
Button brushLarge;
Button brushMeduim;
Button brushSmall;
Button erase;
Button save;
Button trash;
Button rainbow;
Button color1;
Button color2;
Button color3;
this.color1 = ((Button)findViewById(R.id.color1));
this.color2 = ((Button)findViewById(R.id.color2));
this.color3 = ((Button)findViewById(R.id.color3));
this.color1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View paramView)
{
Toast.makeText(getApplicationContext(), "color1", Toast.LENGTH_LONG).show();
//MyView.this.coloringView.setColor("#CD5C5C");
}
});
this.color2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View paramView)
{
//DrawView.changePaintColor(0xFFFF0000);0);
try
{coloringView.changePaintColor(0xFFFF0000);
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), "color1"+e,Toast.LENGTH_LONG).show();
}
//coloringView.changePaintColor(0xFFFF0000);
}
});
this.color3.setOnClickListener(new View.OnClickListener()
{
public void onClick(View paramView)
{
MyView.this.coloringView.setColor("#FA8072");
}
});
return;
}
}
DrawView Class
public class DrawView extends View {
private Paint paint = new Paint();
private Path path = new Path();
private Paint circlePaint = new Paint();
private Path circlePath = new Path();
Canvas mCanvas;
String color = "#ffee22";
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
paint.setAntiAlias(true);
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(15f);
circlePaint.setAntiAlias(true);
circlePaint.setColor(Color.BLUE);
circlePaint.setStyle(Paint.Style.STROKE);
circlePaint.setStrokeJoin(Paint.Join.MITER);
circlePaint.setStrokeWidth(4f);
mCanvas=new Canvas();
}
public void changePaintColor(int color){
this.paint.setColor(color);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
canvas.drawPath(circlePath, circlePaint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// Gives you x and y coordinates on the Event.
float pointX = event.getX();
float pointY = event.getY();
// Checks for the event that occurs
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(pointX, pointY);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(pointX, pointY);
circlePath.reset();
// (circle's center x-coordinate, y-coordinate, radius of the
// circle, direction to wind the shape)
circlePath.addCircle(pointX, pointY, 30, Path.Direction.CW);
//circlePath.addRect(pointX - 25, pointY - 25, pointX + 25, pointY + 25, Path.Direction.CW);
/* RectF rect = new RectF(pointX - 25, pointY - 25, pointX + 25, pointY + 25);
circlePath.addRoundRect(rect, 0, 0, Path.Direction.CW);
*/
break;
case MotionEvent.ACTION_UP:
circlePath.reset();
break;
default:
return false;
}
// Schedules a repaint.
// Force a view to draw.
postInvalidate();
return true;
}
/////function------------------------------------
public void apple()
{
// paint.setColor(ap);
//Toast.makeText(getContext(), "Rehman", Toast.LENGTH_SHORT).show();
this.paint.setColor(0xFF0000FF);
}
public void setColor(String color)
{
this.paint.setColor(0xFF0000FF);
}
public void onSizeChange(int paramInt)
{
paint.setStrokeWidth(paramInt);
}
public void clear()
{
if (this.mCanvas != null)
{
this.paint.setARGB(255, 255, 255, 255);
mCanvas.drawPaint(this.paint);
invalidate();
paint.setColor(Color.parseColor(this.color));
}
}
}