我想使用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