How can I get the context in a fragment?
I need to use my database whose constructor takes in the context, but getApplicationContext()
and FragmentClass.this
don't work so what can I do?
Database constructor
public Database(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
You can call
getActivity()
or,On you fragment
On Activity
Also you can use:
but I would prefer to use
or
You could also get the context from the
inflater
parameter, when overridingonCreateView
.getContext()
came in API 23. Replace it with getActivity() everywhere in the code.See if it fixes the error. Try to use methods which are in between the target and minimun API level, else this error will come in place.