Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static member 'android.content.Context.MODE_PRIVATE' accessed via instance reference

Tags:

java

android

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?

like image 783
Vimbainashe E Mushayikwa Avatar asked May 11 '26 09:05

Vimbainashe E Mushayikwa


1 Answers

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.

like image 105
nikis Avatar answered May 12 '26 23:05

nikis