我想使用SharedPreferences中给出了答案hereSharedPreferences阅读诠释
我想从中读取int值,如下面的代码
int theme= Preferences.readInteger(getApplicationContext(), Preferences.themeNew,"");
但它给我的错误,如下面
readInteger() in Preferences cannot be applied to:
Expected : Actual
Parameter : Arguments
偏好类
public class Preferences {
public static final String PREF_NAME = "your preferences name";
public static final int MODE = Context.MODE_PRIVATE;
public static final int themeNew = 1;
public static final String USER_NAME = "USER_NAME";
public static final String NAME = "NAME";
public static final String EMAIL = "EMAIL";
public static final String PHONE = "PHONE";
public static final String address = "address";
public static void writeBoolean(Context context, String key, boolean value) {
getEditor(context).putBoolean(key, value).commit();
}
public static boolean readBoolean(Context context, String key,
boolean defValue) {
return getPreferences(context).getBoolean(key, defValue);
}
public static void writeInteger(Context context, String key, int value) {
getEditor(context).putInt(key, value).commit();
}
public static int readInteger(Context context, String key, int defValue) {
return getPreferences(context).getInt(key, defValue);
}
public static void writeString(Context context, String key, String value) {
getEditor(context).putString(key, value).commit();
}
public static String readString(Context context, String key, String defValue) {
return getPreferences(context).getString(key, defValue);
}
public static void writeFloat(Context context, String key, float value) {
getEditor(context).putFloat(key, value).commit();
}
public static float readFloat(Context context, String key, float defValue) {
return getPreferences(context).getFloat(key, defValue);
}
public static void writeLong(Context context, String key, long value) {
getEditor(context).putLong(key, value).commit();
}
public static long readLong(Context context, String key, long defValue) {
return getPreferences(context).getLong(key, defValue);
}
public static SharedPreferences getPreferences(Context context) {
return context.getSharedPreferences(PREF_NAME, MODE);
}
public static SharedPreferences.Editor getEditor(Context context) {
return getPreferences(context).edit();
}
}
为什么?
请提供一个完整** **代码例子的字符串。这个行在'onCreate()'方法中?如果是这样,请在代码**中显示**。同时显示封闭类。请注意不要包含与您的问题无关的额外代码。查看[mcve]了解更多有关创建良好代码示例的技巧。 –
共享偏好或只是一个偏好! – Xenolion
我想问题是你正在尝试读取一个整数值,但你设置默认值为“”字符串,而不是一个整数。您应该分享readInteger()方法的确切答案。 – Thracian