Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/java.lang.illegalstateexception: could not execute method for android:onclick - How to fix?
Next
Answered
Tom
  • 3
Tom
Asked: July 12, 20222022-07-12T08:00:45+00:00 2022-07-12T08:00:45+00:00In: java

java.lang.illegalstateexception: could not execute method for android:onclick – How to fix?

  • 3

. Advertisement .

..3..

. Advertisement .

..4..

I need the answer to fix my error: java.lang.illegalstateexception: could not execute method for android:onclick.

I don’t realize why I keep getting errors every time I use the undo and redo buttons.

java.lang.IllegalStateException: Could not execute method of the activity.

Can someone please assist me with this?
Thanks a lots.
My code is shown below, along with its error log.

–Draw Class

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

Error ——

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
  • 1 1 Answer
  • 355 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    dttutoria Expert
    2022-07-12T08:10:56+00:00Added an answer on July 12, 2022 at 8:10 am

    The cause:

    A NullPointerException is noted in your logCat on line 246 of your code. I’ll assume that’s along these lines:

    public void onClick(View view){
    switch (view.getId()){
    case R.id.undoBtn:
    mView.onClickUndo();
    break;
    
    case R.id.redoBtn:
    mView.onClickRedo();
    break;

    The issue “java.lang.illegalstateexception: could not execute method for android:onclick” is because you neglected to initialize mView. Instead, you initialize a local variable called myView in the onCreate method.

    Solution: The solution to your issue should be to assign that instance to mView.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.