I\'m getting this error in my coding and not entirely sure how to resolve this. I\'ve searched to try and resolve this issue but can\'t seem to find anything that works. I\'ve
The error means that there is no getSharedPreferences method in View class, because getSharedPreferences is a method of Context class. In order to access getSharedPreferences method inside View class you need to provide it with an instance of Context class. Something like:
//Instance of Context
Context pref;
SharedPreferences sharedPref = pref.getSharedPreferences(PREFRENCES_NAME,0);
Note: Context pref & String PREFRENCES_NAME should not be null;
Change this code :
SharedPreferences pref = getSharedPreferences(PREFRENCES_NAME,0);
To :
SharedPreferences pref = getActivity().getSharedPreferences(PREFRENCES_NAME,0);
Remember you cant call getSharedPreferences
method directly from Fragment
, because it belongs to the Activity
class. Hence, you just need to call getActivity
.