我在一个活动中创建了共享偏好。该值已存储但当我尝试在另一个活动中访问它时,它只返回默认值。知道我的代码有什么问题吗?
活动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));
答案 0 :(得分:0)
如果您在SharedPrefs
输入值,则需要使用键值格式,这意味着键需要使用引号...如下所示:
editor.putString("Start", start);
你没有使用引号......
当你在getInt()
中找回它时(你已经在那里正确地完成了它)。
编辑:
在你的第一个活动中,你给出的Constant键值等于:&#34; monkey&#34;,&#34; tuekey&#34;等等。
在你的第二个活动中,你用#34;星期一&#34;,星期二和#34;的关键值来呼叫他们。等等。 因此,值不同,您将获得默认值
答案 1 :(得分:0)
您可以像这样设置sharedPreferences的值:
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";
并使用
检索它们 "Monday"
"Tuesday"
"Wednesday"
.
.
etc
这个值必须相等。所以你必须检索它们,例如:
sharedPreferences.getInt("monkey",0);}
或者您必须在您的活动中设置值,如:
public static final String Monday = "Monday";
答案 2 :(得分:0)
您的密钥错误:活动1
public static final String Monday = "monkey";
活动2:
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
当您使用公共静态final String时,您可以使用所有键:
ClassName.MONDAY //this is what I always do no chance for key errors