Android SharedReferences.getInt()在另一个活动中调用时提供默认值

Android SharedReferences.getInt()在另一个活动中调用时提供默认值,android,android-studio,sharedpreferences,Android,Android Studio,Sharedpreferences,我在一个活动中创建了一个共享偏好。正在存储该值,但当我尝试在另一个活动中访问它时,它只返回默认值。知道我的代码有什么问题吗 活动1: public static final String MyPREFERENCES = "MyPrefs" ; public static final String Monday = "monkey"; public static final String Tuesday = "tuekey"; pub

我在一个活动中创建了一个共享偏好。正在存储该值,但当我尝试在另一个活动中访问它时,它只返回默认值。知道我的代码有什么问题吗

活动1:

        public static final String MyPREFERENCES = "MyPrefs" ;
        public static final String Monday = "monkey";
        public static final String Tuesday = "tuekey";
        public static final String Wednesday = "wedkey";
        public static final String Thursday = "thukey";
        public static final String Friday = "frikey";
        public static final String Saturday = "satkey";
        public static final String Sunday = "sunkey";



            sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);   

      String start = ed_start.getText().toString();
            String end = ed_end.getText().toString();

            Calendar c1 = Calendar.getInstance();
            int dd1 = Integer.parseInt(start.substring(0, 2));
            int mm1 = Integer.parseInt(start.substring(3, 5));
            int yy1 = Integer.parseInt(start.substring(6,8));
            c1.set(yy1, mm1, dd1);


            Calendar c2 = Calendar.getInstance();
            int dd2 = Integer.parseInt(end.substring(0, 2));
            int mm2 = Integer.parseInt(end.substring(3, 5));
            int yy2 = Integer.parseInt(end.substring(6,8));
            c2.set(yy2, mm2, dd2);

            int mon = 0;
            int tue = 0;
            int wed = 0;
            int thu = 0;
            int fri = 0;
            int sat = 0;
            int sun = 0;
            c2.add(Calendar.DATE,1);

            while(c2.after(c1)) {
                if(c1.get(Calendar.DAY_OF_WEEK)==Calendar.MONDAY)
                    mon++;
                if(c1.get(Calendar.DAY_OF_WEEK)==Calendar.TUESDAY)
                    tue++;
                if(c1.get(Calendar.DAY_OF_WEEK)==Calendar.WEDNESDAY)
                    wed++;
                if(c1.get(Calendar.DAY_OF_WEEK)==Calendar.THURSDAY)
                    thu++;
                if(c1.get(Calendar.DAY_OF_WEEK)==Calendar.FRIDAY)
                    fri++;
                if(c1.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY)
                    sat++;
                if(c1.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY)
                    sun++;
                c1.add(Calendar.DATE,1);
            }

              SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString(Start, start);
            editor.putString(End,end);
            editor.putInt(Monday,mon);
            editor.putInt(Tuesday,tue);
            editor.putInt(Wednesday,wed);
            editor.putInt(Thursday,thu);
            editor.putInt(Friday,fri);
            editor.putInt(Saturday,sat);
            editor.putInt(Sunday,sun);

            editor.commit();
活动2:

    public static final String MyPREFERENCES = "MyPrefs" ;
    public static final String Monday = "monkey";
public static final String Tuesday = "tuekey";
public static final String Wednesday = "wedkey";
public static final String Thursday = "thukey";
public static final String Friday = "frikey";
public static final String Saturday = "satkey";
public static final String Sunday = "sunkey";


        sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
        int k =0;
        int tot = 0;
        for(;k<strings.length;) {
            if(strings[k].equals("Mon")){
                System.out.println("Moday ent");
                System.out.println(sharedPreferences.getInt("Monday",-1 ));
                tot = tot + sharedPreferences.getInt(Monday, 0); }
            if(strings[k].equals("Tue")) {
                System.out.println("tue ent");
                tot = tot + sharedPreferences.getInt(Tuesday,0);}
            if (strings[k].equals("Wed")) {
                System.out.println("wedday ent");
                tot = tot + sharedPreferences.getInt(Wednesday,0); }
            if (strings[k].equals("Thu"))
                tot = tot + sharedPreferences.getInt(Thursday,0);
            if (strings[k].equals("Fri"))
                tot = tot + sharedPreferences.getInt(Friday,0);
            if(strings[k].equals("Sat"))
                tot = tot + sharedPreferences.getInt(Saturday,0);
            if (strings[k].equals("Sun"))
                tot = tot + sharedPreferences.getInt(Sunday, 0);
            k = k+4;
        }
        System.out.println("tot: " + tot);
        tot = (int)((float)tot*(per/100));
if(strings[k].equals("Mon")){
            System.out.println("Moday ent");
            System.out.println(sharedPreferences.getInt("Monday",-1 ));
            tot = tot + sharedPreferences.getInt("Monday", 0); }

在SharedPref中输入值时,需要使用键值格式,这意味着键值需要用引号括起来。。详情如下:

editor.putString("Start", start);
您没有使用引号

同样,当您在getInt中检索它时,您已经在那里正确地完成了它

祝你好运

编辑:

在第一个活动中,您给出的常量键值等于:monkey、tuekey等

在你的第二个活动中,你用星期一、星期二等关键值来称呼他们。
因此,这些值是不同的,您将获得默认值

您可以这样设置SharedReference的值:

          public static final String Monday = "monkey";
    public static final String Tuesday = "tuekey";
    public static final String Wednesday = "wedkey";
    public static final String Thursday = "thukey";
    public static final String Friday = "frikey";
    public static final String Saturday = "satkey";
    public static final String Sunday = "sunkey";
    sharedPreferences.getInt("monkey",0);}
并用

    "Monday"
    "Tuesday"
    "Wednesday"
       .
       .
      etc
该值必须相等。因此,您必须检索它们,例如:

          public static final String Monday = "monkey";
    public static final String Tuesday = "tuekey";
    public static final String Wednesday = "wedkey";
    public static final String Thursday = "thukey";
    public static final String Friday = "frikey";
    public static final String Saturday = "satkey";
    public static final String Sunday = "sunkey";
    sharedPreferences.getInt("monkey",0);}
或者您必须在活动中设置如下值:

    public static final String Monday = "Monday";

你的钥匙错了:活动1

 public static final String Monday = "monkey";
活动2:

    public static final String MyPREFERENCES = "MyPrefs" ;
    public static final String Monday = "monkey";
public static final String Tuesday = "tuekey";
public static final String Wednesday = "wedkey";
public static final String Thursday = "thukey";
public static final String Friday = "frikey";
public static final String Saturday = "satkey";
public static final String Sunday = "sunkey";


        sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
        int k =0;
        int tot = 0;
        for(;k<strings.length;) {
            if(strings[k].equals("Mon")){
                System.out.println("Moday ent");
                System.out.println(sharedPreferences.getInt("Monday",-1 ));
                tot = tot + sharedPreferences.getInt(Monday, 0); }
            if(strings[k].equals("Tue")) {
                System.out.println("tue ent");
                tot = tot + sharedPreferences.getInt(Tuesday,0);}
            if (strings[k].equals("Wed")) {
                System.out.println("wedday ent");
                tot = tot + sharedPreferences.getInt(Wednesday,0); }
            if (strings[k].equals("Thu"))
                tot = tot + sharedPreferences.getInt(Thursday,0);
            if (strings[k].equals("Fri"))
                tot = tot + sharedPreferences.getInt(Friday,0);
            if(strings[k].equals("Sat"))
                tot = tot + sharedPreferences.getInt(Saturday,0);
            if (strings[k].equals("Sun"))
                tot = tot + sharedPreferences.getInt(Sunday, 0);
            k = k+4;
        }
        System.out.println("tot: " + tot);
        tot = (int)((float)tot*(per/100));
if(strings[k].equals("Mon")){
            System.out.println("Moday ent");
            System.out.println(sharedPreferences.getInt("Monday",-1 ));
            tot = tot + sharedPreferences.getInt("Monday", 0); }
请看这一行:

getInt("Monday",-1 ));
应该是:

getInt("monkey",-1 ));//similarly for all the fields
当您对所有可以使用的键使用公共静态最终字符串时:

ClassName.MONDAY //this is what I always do no chance for key errors

MyPREFERENCES在这两种情况下是否相同?星期一、星期二等的值是什么?您在SharedReferences上传递值的位置是什么?不是posted@Opiatefuchs:添加了…不,你不需要它。如果声明一个字符串变量,如private string Monday=Monday,“你可以使用itSo,它应该是一个注释。”荣格博伊没有。我的回答是基于他发布的代码。如果没有贴出来,那可能是他犯了一个错误,这可能就是答案。顺便说一下,lol在整个代码中都改变了它,这是非法的。SharedReference中有很多值。尝试活动1.第二个活动中的星期一