简体   繁体   English

通过实例引用访问静态成员'android.content.Context.MODE_PRIVATE'

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

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. 我一直收到一条错误消息,说Static member 'android.content.Context.MODE_PRIVATE' accessed via instance reference但问题是错误是非常不清楚我不太明白错误的含义。它在我的NavigationDrawerFragment弹出两次类文件。 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. 因为在后一种情况下, context是示例中的Context实例。

static variables and static method should be accessed through class name . static variablesstatic method应该通过class name访问。 here MODE_PRIVATE is constant variable (static, final). 这里MODE_PRIVATE是常量变量(静态,最终)。

so you need to access as 所以你需要访问

Context.MODE_PRIVATE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM