我正在尝试使用共享首选项,如这里给出的答案
我正在尝试从中读取 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();
}
}
为什么?
你应该使用:
String KEY_VALUE = "MY_KEY";
int not_found = -1;
int theme = getPreferences(MODE_PRIVATE).getInt(KEY_VALUE,not_found);
其中MODE_PRIVATE表示您正在使用的操作模式(MODE_PRIVATE是默认模式)。
在您的情况下,not_found应该是default_theme。
我认为您的代码不起作用,因为您在预期 int 时发送字符串值(your_default值处预期为 int,但您使用的是字符串)。您的代码构建是否成功?
ps:如果您提供课程,我们可以提供更好的帮助。
您的问题是为 int 值设置字符串值。
public static int readInteger(Context context, String key, int defValue) {
return getPreferences(context).getInt(key, defValue);
}
此方法要求 defValue 是一个 int 但Preferences.readInteger(getApplicationContext(), Preferences.themeNew,"");
," 不是 int 值,它是一个长度为零的字符串
问题是静态方法readInteger()
没有最后一个参数作为String
而是作为int
,所以请同时更改此行themeNew
也应该String
key
用于检索Integer
主题:
int theme= Preferences.readInteger(getApplicationContext(), Preferences.themeNew,"");
要这样:
int theme= Preferences.readInteger(getApplicationContext(), "my_theme",0);
其中0
是默认值,如果未找到您的首选项!祝您编码愉快!