Cannot find symbol for makeText method in the Toast class?

Viewed 32
public void showToast(View view) {
    Toast toast = new Toast.makeText(this, R.string.toast_message, Toast.LENGTH_SHORT);
    toast.show();
}
error: cannot find symbol
        Toast toast = new Toast.makeText(this, R.string.toast_message, Toast.LENGTH_SHORT);
                               ^
  symbol:   class makeText
  location: class Toast

As far as I can tell everything looks right, but for some reason I still keep getting this error.

3 Answers

Toast toast = new Toast.makeText(...); It is a constructor invocation, and Toast class doesn't exist. So it won't work.

You need to write only

Toast.makeText(this, R.string.toast_message, Toast.LENGTH_SHORT).show();

For more information you may visit android docs

The problem is new Toast.makeText(..). This is a constructor invocation for a class makeText nested in Toast, such a class does not exist. You need to call the method makeText of Toast:

Toast toast = Toast.makeText(this, R.string.toast_message, Toast.LENGTH_SHORT);

Toast.makeText is a method, not a class. Remove the new to call the method rather than trying to instantiate a class.

Related