0

So I am learning how to make an android app and I am trying to make a method that shows a simple toast message and I have the following code:

    public void showMessage(String message){
        Toast.makeText(this.context, message, Toast.LENGTH_SHORT);
    }

and I get the error:

Cannot resolve method 'makeText(Context, java.lang.String, int)'

this.context is a Context object and I can see according to the documentation on android studio that makeText(Context, CharSequence, int) is a valid method, but it won't accept it. I've tried using CharSequence as my parameter instead of String and I get the same error. What am I doing wrong?

Dyskord
  • 365
  • 5
  • 14

1 Answers1

1

You can use context passed within constructor to getApplicationContext().

Something like this

public void showMessage(String message){
        Toast.makeText(context.getApplicationContext(), message, Toast.LENGTH_SHORT);
    }
Aderoju Israel
  • 154
  • 1
  • 11