The most likely cause is because the fragment has not yet attached to the activity. You need to call getActivity()
in the fragment’s onActivityCreated()
method.
void onActivityCreated (Bundle savedInstanceState){ //call getActivity() here }
solved How to resolve NullPointerException in SharedPreference? [duplicate]