Android: Exception when running Toast.makeText

Asked

Viewed 246 times

2

Ola, I am getting an exception when the Toast.makeText:

public void onClick(View view){
(...)
CharSequence text1 = "Please insert a number.";
 int id= view.getId();
(...)
    else if(id== R.id.buttonOK){
                if(editText.getText().toString().matches(""))
                {
                    Toast.makeText(view.getContext(), text1, Toast.LENGTH_SHORT).show();
                }

I’ve tried to use it as a first argument:

getApplicationContext()

this

Myactivity.this

view getcontext()

But I always get the same exception. I have also changed text1 and continue to get the exception. I put a Log. d inside the IF to see if the problem was in the IF, but I can get inside the IF, the exception is generated in Toast.makeText.

Exception:

java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:4007)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4002)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)

Any idea how to fix this?

  • tried to use getActivity().getBaseContext() ?

  • 1

    @sicachester already solved my problem and had nothing to do with Toast was a bug in IF-ELSE : Thanks anyway!

  • I’m glad you decided !! contribute to the community, post your solution as answer !

1 answer

1

Your mistake was indentation:

public void onClick(View view){
(...)
CharSequence text1 = "Please insert a number.";
 int id= view.getId();
(...)
    else if(id== R.id.buttonOK){
                if(editText.getText().toString().matches(""))
                {
                    Toast.makeText(view.getContext(), text1, Toast.LENGTH_SHORT).show();
                }

Correct:

public void onClick(View view){
(...)
CharSequence text1 = "Please insert a number.";
 int id= view.getId();
(...)

if(ALGUMA COISA){

...

} else if(id== R.id.buttonOK){
                if(editText.getText().toString().matches(""))
                {
                    Toast.makeText(view.getContext(), text1, Toast.LENGTH_SHORT).show();
                }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.