Java 读取共享引用时的奇怪行为

Java 读取共享引用时的奇怪行为,java,android,sharedpreferences,Java,Android,Sharedpreferences,当我启动应用程序并运行onResume()方法时,从我的SharedReferences读取时出现了一些问题。这就是代码的外观 static double cowCount = 197, income, cowMult = 1; ... protected void onResume() { super.onResume(); SharedPreferences sharedPref = getSharedPreferences("com.example.cowcount",

当我启动应用程序并运行onResume()方法时,从我的SharedReferences读取时出现了一些问题。这就是代码的外观

static double cowCount = 197, income, cowMult = 1;
...
protected void onResume() { 
    super.onResume();
    SharedPreferences sharedPref = getSharedPreferences("com.example.cowcount", Context.MODE_PRIVATE);
    cowCount = sharedPref.getFloat("cowCount", 0);
    cowMult = sharedPref.getFloat("cowMult", 0);
    income = sharedPref.getFloat("income", 0);

 }
...
当代码是这样时,应用程序被冻结。这个应用程序由一个计数器组成,当我按下应该计数的按钮时,什么也没有发生

但是,当我注释掉从SharedReferences中为cowmultdouble赋值的行时,应用程序不会冻结

cowCount = sharedPref.getFloat("cowCount", 0);
// cowMult = sharedPref.getFloat("cowMult", 0);
income = sharedPref.getFloat("income", 0);
要明确的是,上述方法效果良好

这是按下按钮时调用的方法(该按钮应将cowCount的值提高1):


您发布的代码有两件事很奇怪

你为什么打电话来

SharedPreferences sharedPref = getSharedPreferences(null, Context.MODE_PRIVATE);
而不是

SharedPreferences sharedPref = getSharedPreferences( "com.myname.myapp", Context.MODE_PRIVATE);
你的简历应该如下

super.onResume();
SharedPreferences sharedPref = getSharedPreferences(null, Context.MODE_PRIVATE);
cowCount = Double.longBitsToDouble(sharedPref.getLong("cowCount", 0));
cowMult = Double.longBitsToDouble(sharedPref.getLong("cowMult", 0));
income = Double.longBitsToDouble(sharedPref.getLong("income", 0));
在代码之前首先调用super.onResume()(与所有生命周期方法相同)

编辑 3.为什么你不只是把你的值设置为int(从你上面说的看起来是)或float,这可能会给你所有的精度,然后你可以使用

getInt(String key, int defValue)

辅助编辑 我可以看到代码中有一些奇怪的方式。试试下面的代码,告诉我它是否解决了这个问题(尽管我看不出SharedReferences是如何导致这个问题的)。我假设addCow方法是从onClickListener调用的

//get a reference for your myTextView in the onCreate() method, after declaring your variable

//outside the onCreate method i.e
TextView myTextView;
...
// int onCreate()
 myTextView = (TextView)findViewById(R.id.myText);

//you don't need to pass a view parameter, so don't
public void addCow () {
    cowCount = cowCount + cowMult;
    refresh();
}
...
public void refresh () {
  //the way you are getting a string value is also not what I would do either use
  //Float.toString(cowCount) or just
   myTextView.setText("You Have " + cowCount + " Cows!"));
}

希望问题消失。

按如下方式更改代码:

static float cowCount, income, cowMult;
...
protected void onResume()
{
    super.onResume();
    SharedPreferences sharedPref = getSharedPreferences("com.example.cowcount", Context.MODE_PRIVATE);
    cowCount = sharedPref.getFloat("cowCount", 197);
    cowMult = sharedPref.getFloat("cowMult", 1);
    income = sharedPref.getFloat("income", 0);
}
...
SharedReferences.getFloat()
中的第二个参数是默认值,如果找不到键,该方法将返回该值。对于您提供的代码,如果您没有正确地将值保存到
SharedReferences
,则这些变量的值将被指定为0。这就是为什么当你按下按钮时什么都没有改变;您正在添加0。检查以确保正确保存到
SharedReferences

此外,在声明变量时初始化变量也没有意义,因为它们都在
onResume
方法中被分配了一个值,无论是保存的值还是默认值


正如Martin所指出的,在
onCreate
方法中指定
TextView

好的,我尝试了浮点值,但同样的问题仍然存在。注释掉“cowMult=…”行仍然会使应用程序运行。。。顺便说一句,我不能使用Int,因为是小数。如果不了解按钮代码的工作方式,我想我也帮不了你。当我更改TextView时,在类的开头声明它并在onCreate()中分配它,应用程序在启动时崩溃。一些NullPointException的东西。现在它工作了。。。我必须删除刷新();来自onCreate()。不管怎样,它每秒被调用10次,所以这不是问题。。。非常感谢您的帮助,Martin。应用程序如何“冻结”?你一定要用任务管理器杀死它吗?或者你的意思是你可以按下按钮,但是myTextView没有改变?所以它就是这样工作的!您觉得填写0不正确。。。非常感谢你!
//get a reference for your myTextView in the onCreate() method, after declaring your variable

//outside the onCreate method i.e
TextView myTextView;
...
// int onCreate()
 myTextView = (TextView)findViewById(R.id.myText);

//you don't need to pass a view parameter, so don't
public void addCow () {
    cowCount = cowCount + cowMult;
    refresh();
}
...
public void refresh () {
  //the way you are getting a string value is also not what I would do either use
  //Float.toString(cowCount) or just
   myTextView.setText("You Have " + cowCount + " Cows!"));
}
static float cowCount, income, cowMult;
...
protected void onResume()
{
    super.onResume();
    SharedPreferences sharedPref = getSharedPreferences("com.example.cowcount", Context.MODE_PRIVATE);
    cowCount = sharedPref.getFloat("cowCount", 197);
    cowMult = sharedPref.getFloat("cowMult", 1);
    income = sharedPref.getFloat("income", 0);
}
...