Easy way
just declare a variable as shown below
private Context context;
and onCreate (), assign its value as shown below,
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); context = this; ... }
You can also assign in another way as follows:
context = getApplicationContext();
You can get the context by calling getApplicationContext (), getContext (), getBaseContext () or this (when in the activity class).
source share