Java 无法从首选项(设置)检索字符串

Java 无法从首选项(设置)检索字符串,java,android,sharedpreferences,Java,Android,Sharedpreferences,我在Android应用程序中遇到了一个问题。问题是由于某种原因,我无法从我的preferences.xml文件中检索ListPreference的字符串。以下是我尝试的代码: private void showUserSettings() { SharedPreferences sharedPrefs = PreferenceManager .getDefaultSharedPreferences(this); try { String

我在Android应用程序中遇到了一个问题。问题是由于某种原因,我无法从我的
preferences.xml
文件中检索
ListPreference
的字符串。以下是我尝试的代码:

private void showUserSettings() {
    SharedPreferences sharedPrefs = PreferenceManager
            .getDefaultSharedPreferences(this);

    try {

        String feet = sharedPrefs.getString("feet", "NULL");
        int intfeet = Integer.parseInt(feet);
        int intinches = Integer.parseInt(sharedPrefs.getString("feet", "NULL").trim());
        int weight = Integer.parseInt(sharedPrefs.getString("weight", "NULL").trim());
        int age = Integer.parseInt(sharedPrefs.getString("age", "NULL").trim());
        double calories;
        double inches = ((intfeet * 12) + intinches);
        if (gender.equals("Female")) {
            //BMR = 655 + ( 4.35 x weight in pounds ) + ( 4.7 x height in inches ) - ( 4.7 x age in years )
            calories = 655 + (4.35 * weight) + (4.7 * inches) - (4.7 * age);
        }
        else {
            //66 + ( 6.23 x weight in pounds ) + ( 12.7 x height in inches ) - ( 6.8 x age in years )
            calories = 66.5 + (6.23 * weight) + (12.7 * inches) - (6.8 * age);
        }
        //ListPreference listPreference = (ListPreference) findPreference("exercise");
        //CharSequence currText = listPreference.getEntry();
        String exercise = sharedPrefs.getString("exercise", "NULL").toString();
        if (exercise.equals("Sedentary")) {
            double sedentary = calories * 1.2;
            Toast.makeText(getApplicationContext(), sedentary + "", Toast.LENGTH_LONG).show();
        }
        if (exercise.equals("Light")){
            double light = calories * 1.375;
            Toast.makeText(getApplicationContext(), light + "", Toast.LENGTH_LONG).show();
        }
        if (exercise.equals("Moderate")){
            double moderate = calories * 1.55;
            Toast.makeText(getApplicationContext(), moderate + "", Toast.LENGTH_LONG).show();
        }
        if (exercise.equals("Very active")) {
            double veryactive = calories * 1.725;
            Toast.makeText(getApplicationContext(), veryactive + "", Toast.LENGTH_LONG).show();
        }
        if (exercise.equals("Extra active")) {
            double extraactive = calories * 1.9;
            Toast.makeText(getApplicationContext(), extraactive + "", Toast.LENGTH_LONG).show();
        }
        else {
            Toast.makeText(getApplicationContext(), "Uh oh", Toast.LENGTH_SHORT).show();
        }
    }catch (Exception e) {
        e.printStackTrace();
    }
}

每当它到达第二条if/else语句时,它就直接转到else语句。我不知道它为什么这样做。我搜索了SO和Google,但没有结果。我基本上撞到了砖墙。非常感谢您对这个问题的任何帮助。

您所有的练习条件都是独立的,而else只与最后一个if语句关联。使用
else if
以我相信你想要的方式将它们绑定在一起。

你是说当它到达这里时,
if(exercise.equals(“Extra-active”){
,它只会转到
else
?你设置了一个断点来查看
exercise
的值吗?另外,你不需要调用
toString()
字符串上的