Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot resolve method 'getApplicationContext()' in Fragment class [duplicate]

Tags:

java

android

private void showToast(String message)
{
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}

Should I change to getActivity()?

like image 579
lydia sanusi Avatar asked Aug 21 '16 16:08

lydia sanusi


1 Answers

getApplicationContext() must be called on an instance of a Context class, so yes, you should call:

getActivity().getApplicationContext()

You can also just pass getActivity as the first parameter since the activity itself extends Context class.

like image 76
Vucko Avatar answered Oct 19 '22 23:10

Vucko