每次点击撤消并重做时我都会出错,我不知道为什么会出现这个错误
java.lang.IllegalStateException: Could not execute method of the activity.
任何人都可以帮助我。 提前致谢。 下面是我的代码及其错误日志
- 绘制班级
public class Draw extends Activity implements {
public static String filex;
private static final int SELECT_PICTURE = 1;
public static String selectedImagePath;
public static Bitmap cBitmap;
public static Integer resize;
public static Integer imageBackgroundEraser;
// Instance variables
public static Paint mPaint;
private MaskFilter mEmboss;
private MaskFilter mBlur;
private MyView mView;
private int mImageCount = 0;
private int mLastBg = 0;
public static Integer WW;
public static Integer WH;
private float BrushWidth;
private int thickness;
private int BrushColor;
public static int backgroundColor;
float Mx1,My1;
float x,y;
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_draw);
Display display = getWindowManager().getDefaultDisplay(); //***
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_draw4_fun_main);
Display display = getWindowManager().getDefaultDisplay(); //***
WH = display.getHeight(); //***
WW = display.getWidth(); //***
final MyView myView = new MyView(this);
frm_layout=(FrameLayout) findViewById(R.id.main_frame);
frm_layout.addView(myView);
setInitialPaint();
backgroundColor = Color.WHITE;
}
private void setInitialPaint() {
//bgColor = 0xFFFFFFFF; // default bg color white
BrushWidth = 3;
BrushColor = 0xFF000000; //**
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(BrushColor);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(BrushWidth);
mPaint.setPathEffect(new CornerPathEffect(30) );
//mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },0.4f, 6, 3.5f);
//mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
}
public void onClick(View view){
switch (view.getId()){
case R.id.undoBtn:
mView.onClickUndo();
break;
case R.id.redoBtn:
mView.onClickRedo();
break;
case R.id.saveBtn:
File myDir=new File("/sdcard/Pictures/Draw");
myDir.mkdirs();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date(System.currentTimeMillis())); //***
String fname = timeStamp + ".png";
File file = new File (myDir, fname);
System.out.println(file+"----> file");
if (file.exists ()) file.delete ();
saveAsPng(file);
break;
}
}
public void saveAsPng (File f)
{
String fname = f.getAbsolutePath ();
FileOutputStream fos = null;
try {
fos = new FileOutputStream (f);
mView.mBitmap.compress (CompressFormat.PNG, 100, fos);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
Toast.makeText (getApplicationContext(), "Saved " + fname, Toast.LENGTH_LONG).show ();
} catch (Throwable ex) {
Toast.makeText (getApplicationContext(), "Error: " + ex.getMessage (), Toast.LENGTH_LONG).show ();
ex.printStackTrace ();
}
} // end saveAsPng
} // end Main
--- MyView Class
public class MyView extends View {
/**
*/
// Constants and variables
private static final float MINP = 0.25f;
private static final float MAXP = 0.75f;
public static Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
private ArrayList<Path> paths = new ArrayList<Path>();
private ArrayList<Path> undonePaths = new ArrayList<Path>();
// public int backgroundColor;
/**
*/
public MyView(Context c)
{
super(c);
/*if (Draw.selectedImagePath == null) {*/
setFocusable(true);
setFocusableInTouchMode(true);
// Figure out how this works. Seems odd that we set up a canvas and a bitmap.
// I don't see how it connects to what shows on screen.
mBitmap = Bitmap.createBitmap(Draw4FunMain.WW, Draw4FunMain.WH, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
// mCanvas.setBackgroundResource(0xFFFFFFFF);
if (Draw.imageBackgroundEraser == null) {
mCanvas.drawColor (Color.WHITE);
} else {
mCanvas.drawColor (Color.TRANSPARENT);
}
mCanvas.drawColor(Color.TRANSPARENT);
}
public MyView (Context c, int color)
{
super(c);
setFocusable(true);
setFocusableInTouchMode(true);
mBitmap = Bitmap.createBitmap(Draw.WW, Draw.WH, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mCanvas.drawColor (color);
}
/**
*/
// Methods
/**
*/
@Override protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
super.onSizeChanged(w, h, oldw, oldh);
}
/**
*/
@Override protected void onDraw(Canvas canvas)
{
// canvas.drawColor(Color.TRANSPARENT);
canvas.drawBitmap(mBitmap, 0, 0, null);
for (Path p : paths){
canvas.drawPath(p, Draw.mPaint);
//System.out.println("p-->" + p);
// System.out.println("Draw.mPaint-->" + Draw4FunMain.mPaint);
}
canvas.drawPath(mPath, Draw.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, Draw.mPaint);
// kill this so we don't double draw
mPath.reset();
paths.add(mPath);
mPath = new Path();
System.out.println("path-->" + paths);
System.out.println("size-->" + paths.size());
}
/**
*/
public void onClickUndo () {
if (paths.size()>0)
{
undonePaths.add(paths.remove(paths.size()-1));
invalidate();
}
else
{
}
//toast the user
}
public void onClickRedo (){
if (undonePaths.size()>0)
{
paths.add(undonePaths.remove(undonePaths.size()-1));
invalidate();
}
else
{
}
}
@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;
}
} // end MyView
错误------
11-14 14:55:39.022: E/AndroidRuntime(1728): FATAL EXCEPTION: main
11-14 14:55:39.022: E/AndroidRuntime(1728): java.lang.IllegalStateException: Could not execute method of the activity
11-14 14:55:39.022: E/AndroidRuntime(1728): at android.view.View$1.onClick(View.java:3591)
11-14 14:55:39.022: E/AndroidRuntime(1728): at android.view.View.performClick(View.java:4084)
11-14 14:55:39.022: E/AndroidRuntime(1728): at android.view.View$PerformClick.run(View.java:16966)
11-14 14:55:39.022: E/AndroidRuntime(1728): at android.os.Handler.handleCallback(Handler.java:615)
11-14 14:55:39.022: E/AndroidRuntime(1728): at android.os.Handler.dispatchMessage(Handler.java:92)
11-14 14:55:39.022: E/AndroidRuntime(1728): at android.os.Looper.loop(Looper.java:137)
11-14 14:55:39.022: E/AndroidRuntime(1728): at android.app.ActivityThread.main(ActivityThread.java:4899)
11-14 14:55:39.022: E/AndroidRuntime(1728): at java.lang.reflect.Method.invokeNative(Native Method)
11-14 14:55:39.022: E/AndroidRuntime(1728): at java.lang.reflect.Method.invoke(Method.java:511)
11-14 14:55:39.022: E/AndroidRuntime(1728): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
11-14 14:55:39.022: E/AndroidRuntime(1728): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
11-14 14:55:39.022: E/AndroidRuntime(1728): at dalvik.system.NativeStart.main(Native Method)
11-14 14:55:39.022: E/AndroidRuntime(1728): Caused by: java.lang.reflect.InvocationTargetException
11-14 14:55:39.022: E/AndroidRuntime(1728): at java.lang.reflect.Method.invokeNative(Native Method)
11-14 14:55:39.022: E/AndroidRuntime(1728): at java.lang.reflect.Method.invoke(Method.java:511)
11-14 14:55:39.022: E/AndroidRuntime(1728): at android.view.View$1.onClick(View.java:3586)
11-14 14:55:39.022: E/AndroidRuntime(1728): ... 11 more
11-14 14:55:39.022: E/AndroidRuntime(1728): Caused by: java.lang.NullPointerException
11-14 14:55:39.022: E/AndroidRuntime(1728): at com.draw.Draw4FunMain.onClick(Draw.java:246)
11-14 14:55:39.022: E/AndroidRuntime(1728): ... 14 more
答案 0 :(得分:3)
您的logCat在代码的第246行指出了NullPointerException。我将假设这些是:
public void onClick(View view){
switch (view.getId()){
case R.id.undoBtn:
mView.onClickUndo();
break;
case R.id.redoBtn:
mView.onClickRedo();
break;
问题是,您没有初始化mView
。在onCreate
中,您可以初始化本地变量myView
。将该实例分配给mView
可以解决您的问题。
答案 1 :(得分:1)
是。我也认为Paul-Jan是正确的。以下行
final MyView myView = new MyView(this);
在Draw.java中的onCreate方法的应该只用。
myView = new MyView(this);