Android: Exception when executing Toast.makeText

2

Hello, I'm getting an exception when Toast.makeText is executed:

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 have already tried to use as the first argument:

getApplicationContext ()

this

MyActivity.this

view.getContext ()

But I always get the same exception. I've already changed the text1 and I still 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 the 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 ideas for solving this?

    
asked by anonymous 02.03.2015 / 18:33

1 answer

1

Your mistake was an 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();
                }
    
04.03.2015 / 20:40