Sorry I didn't quite know how to rephrase the title cause the error isn't very clear.
I keep getting an error message saying Static member 'android.content.Context.MODE_PRIVATE' accessed via instance reference but the problem is the error is very unclear and I don't quite understand what the error means.It pops up twice in my NavigationDrawerFragment class file. Here is my code which it pops up in.
public static void saveToPreferences(Context context, String preferenceName, String preferenceValue){
SharedPreferences sharedPreferences= context.getSharedPreferences(PREF_FILE_NAME, context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString(preferenceName,preferenceValue);
editor.apply();
}
public static String readFromPreferences(Context context, String preferenceName, String defaultValue){
SharedPreferences sharedPreferences= context.getSharedPreferences(PREF_FILE_NAME, context.MODE_PRIVATE);
return sharedPreferences.getString(preferenceName, defaultValue);
}
What does the error mean and how can I resolve it?
This is a static field, so you need to access it via class reference:
Context.MODE_PRIVATE
instead of:
context.MODE_PRIVATE
because in the latter case context is an instance of Context in your example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With