Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SharedReferences读取整数_Java_Android_Sharedpreferences - Fatal编程技术网

Java SharedReferences读取整数

Java SharedReferences读取整数,java,android,sharedpreferences,Java,Android,Sharedpreferences,我试图使用共享引用,如给出的答案所示 我试图从中读取int值,如下代码所示 int theme= Preferences.readInteger(getApplicationContext(), Preferences.themeNew,""); 但它给我的错误如下 readInteger() in Preferences cannot be applied to: Expected : Actual Parameter : Arguments 优先等级 public class Pref

我试图使用共享引用,如给出的答案所示

我试图从中读取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是默认模式)

在您的情况下,“未找到”应该是默认主题。

我认为您的代码不起作用,因为您正在发送一个字符串值,而需要一个int(默认值需要一个int,但您正在使用一个字符串)。您的代码构建成功了吗


ps:如果您提供您的类,我们可以提供更好的帮助。

问题在于静态方法
readInteger()
的最后一个参数不是
String
,而是
int
,因此请将此行更改为
themeNew
也应该是
String
键,用于检索
整数
主题:

    int theme= Preferences.readInteger(getApplicationContext(), Preferences.themeNew,"");
为此:

    int theme= Preferences.readInteger(getApplicationContext(), "my_theme",0);

其中
0
是未找到您的首选项时的默认值!快乐编码

您的问题是为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值,它是长度为零的字符串

请提供完整的代码示例。此行是否在
onCreate()方法中?如果是,请在代码中显示。还显示封闭类。注意不要包含与您的问题无关的额外代码。查看更多关于创建好代码示例的提示。共享首选项或只是一个首选项!我认为问题是您试图读取一个整数值,但您将默认值设置为“”字符串而不是int。您应该分享readInteger()方法的作用,以获得准确答案。@FatihOzcan我将int设置为值。请检查“代码添加”是否未将默认值设置为int.getPreferences(context).getInt(key,defValue)。defValue是字符串。“”不是整数什么是KEY\u值,这里的默认值?KEY\u值是用于保存值的字符串,如“MY\u integer”如果首选项中不存在key_值,那么默认值就是您得到的值这不是问题的答案问题是您传递的是字符串,而不是默认的整数@Priya。检查我的答案上面这不是它!对不起。@Xenolion我的答案是这样的,但不是粗体。现在我得到了你@ThiagoSouto你也对了!我的俯瞰,对不起!它给我的错误称为错误的第二个参数类型。find int required java.lang.StringYes第二个参数应该是键的字符串名称。我已经更新了答案来修改它@请注意,OP还传递一个
int
,其中第二个参数中应包含
字符串。