Look at the Fragment Lifecycle. Wait until the Fragment is attached to the Activity
http://developer.android.com/guide/components/fragments.html
You can do your work in onActivityCreated
.
To get Context
use getActivity()
protected void onPostCreate (Bundle savedInstanceState)
Added in API level 1
Called when Activity start-up is complete (after onStart()
and onRestoreInstanceState(Bundle)
have been called). Applications
will generally not implement this method; it is intended for system
classes to do final initialization after application code has run.
Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be
thrown.
Parameters savedInstanceState
If the Activity is being re-initialized after previously being shut down then this Bundle
contains the data it most recently supplied in
onSaveInstanceState(Bundle)
. Note: Otherwise it is null.