Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SharedReferences始终返回自定义对象的默认值_Java_Android_Sharedpreferences - Fatal编程技术网

Java SharedReferences始终返回自定义对象的默认值

Java SharedReferences始终返回自定义对象的默认值,java,android,sharedpreferences,Java,Android,Sharedpreferences,我正在制作一个简单的预算跟踪应用程序。我有一个Budget类,它存储几个变量:预算的金额、持续时间和初始值。我在片段中只有一个全局预算对象,称为“预算”,我正试图保存它。它似乎保存得很好,但当我尝试检索它时,由于某种原因,它返回默认值。以下是我保存和加载的方法。我只在onCreateView中调用getBudget(),并且只在onResume()中以及编辑预算后调用saveBudget。注意日志条目 public void saveBudget(){ SharedPreferences

我正在制作一个简单的预算跟踪应用程序。我有一个Budget类,它存储几个变量:预算的金额、持续时间和初始值。我在片段中只有一个全局预算对象,称为“预算”,我正试图保存它。它似乎保存得很好,但当我尝试检索它时,由于某种原因,它返回默认值。以下是我保存和加载的方法。我只在onCreateView中调用getBudget(),并且只在onResume()中以及编辑预算后调用saveBudget。注意日志条目

public void saveBudget(){
    SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.clear();
    Gson gson = new Gson();
    String json = gson.toJson(budget);
    Log.d("BTAG", "JSON: " + json);
    editor.putString("current budget", json);
    editor.commit();
 }

public Budget getBudget(){
    SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
    Gson gson = new Gson();
    String json = sharedPref.getString("current budget", null);
    Log.d("BTAG", "gson from json: " + gson.fromJson(json, Budget.class));
    return gson.fromJson(json, Budget.class);
}
我的日志显示我当前的预算实例:

D/BTAG: JSON: {"amount":35.92,"frequency":"monthly","originalAmount":35.92}
D/BTAG: gson from json: null

这表明它保存时没有问题,但加载不起作用。为什么getBudget()不起作用?

我认为问题可能出在:

仅在
onResume()中调用
saveBudget

  • 当应用程序关闭时,您必须保存预算,而不是在
    onResume()
  • 另外,我认为不应该使用硬编码字符串作为键。尝试使用字符串资源,如中所示。如果您在“键”中出错,您将获得默认值
  • 如果保存预算,然后将其加载到另一个活动中,则问题可能在于获取
    SharedReferences
    实例。要获取
    SharedReferences
    使用:
  • 你可以试试这个,我希望它能编译:

    public static final String SHARED_PREFS = "NAME_OF_SP";
    public static final String NAME_OF_VAL = "budget";
    
    public void saveBudget()
    {
        SharedPreferences sharedPref = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        Gson gson = new Gson();
        String json = gson.toJson(budget);
        Log.d("BTAG", "JSON: " + json);
        editor.putString(NAME_OF_VAL, json);
        editor.apply();
    }
    
    public Budget getBudget()
    {
        SharedPreferences sharedPref = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
        Gson gson = new Gson();
        String json = sharedPref.getString(NAME_OF_VAL, null);
        Log.d("BTAG", "gson from json: " + gson.fromJson(json, Budget.class));
        return gson.fromJson(json, Budget.class);
    }
    

    你能在
    getBudget()
    中记录
    json
    字符串吗?可能问题在于转换过程中,您从共享首选项接收到了正确的字符串。刚刚选中,它在getBudget tooI中显示json为null,实际上它是在Resume()上显示的,因为预算的输入是在另一个活动中提供的。我只是把它放在onPause()和onDestroy()中,但还是一样的问题。@Drypoto所以你在不同的活动中使用
    saveBudget
    getFunction
    ?不,所有的保存和获取都是在我的BudgetFragment.java类中完成的。我有另一个类createBudget.java,我在其中调用BudgetFragment.budget来设置用户输入的变量。所以在另一个活动中,我仍然访问BudgetFragment中的静态预算变量。这非常有效!非常感谢你!你知道为什么这样做有效而我的实现没有成功吗?@Drypoto我很高兴!我不确定,但我改变了三件事。我删除了
    editor.clear()。已更改
    editor.commit()
    编辑器.apply()。并更改获取
    SharedReferences
    实例的方式。您可以尝试在代码中只更改其中一项,然后您就会知道问题的确切位置。
    public static final String SHARED_PREFS = "NAME_OF_SP";
    public static final String NAME_OF_VAL = "budget";
    
    public void saveBudget()
    {
        SharedPreferences sharedPref = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        Gson gson = new Gson();
        String json = gson.toJson(budget);
        Log.d("BTAG", "JSON: " + json);
        editor.putString(NAME_OF_VAL, json);
        editor.apply();
    }
    
    public Budget getBudget()
    {
        SharedPreferences sharedPref = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
        Gson gson = new Gson();
        String json = sharedPref.getString(NAME_OF_VAL, null);
        Log.d("BTAG", "gson from json: " + gson.fromJson(json, Budget.class));
        return gson.fromJson(json, Budget.class);
    }