此函数返回0.0,但是所有值都是float,并且可以从共享首选项中正确检索!可能是什么错误?
float BMR(){
SharedPreferences customSharedPreference = getSharedPreferences(
"myCustomSharedPrefs", Activity.MODE_PRIVATE);
String Weight =customSharedPreference.getString("weightpref", null);
float weight = Float.valueOf(Weight);
String Height = customSharedPreference.getString("heightpref",null);
float height = Float.valueOf(Height);
String Age = customSharedPreference.getString("agepref", null);
float age = Float.valueOf(Age);
int gender = customSharedPreference.getInt("genderpref", 0);
Toast.makeText(StatsActivity.this, Age, Toast.LENGTH_LONG).show();
if ( gender == 0){
bmr = (float) (10.0 *weight + 6.25 * height - 5.0 * age + 5.0);
}
if(gender == 1){
bmr = (float) (10.0 * weight + 6.25 * height - 5.0 * age- 161.0);
}
String bmrtest = String.valueOf(bmr);
Toast.makeText(StatsActivity.this, "bmr is "+bmrtest, Toast.LENGTH_LONG).show();
return bmr;
}
答案 0 :(得分:2)
如果性别不是0或1,会发生什么?你的bmr将保持未分配状态(至0.0)。
如果这是问题,您可以考虑将来: