【问题标题】:Android SharedPreferences not working as expectedAndroid SharedPreferences 未按预期工作
【发布时间】:2014-09-22 13:44:00
【问题描述】:

我想在 SharedPreferences 中保存一些用户设置,但不知何故,当我读回来时,保存的字符串总是空的。

正在保存..

SharedPreferences prefs = getApplicationContext().getSharedPreferences("AAAAPrefs", Activity.MODE_PRIVATE);
                    SharedPreferences.Editor edits = prefs.edit();
                    edits.putString("request_id", requestId);
                    edits.putInt("active", active);
                    edits.commit();

阅读..

SharedPreferences prefs = getApplicationContext().getSharedPreferences("AAAAPrefs", Activity.MODE_PRIVATE);
                    String request_id = prefs.getString("request_id", "");

我做错了什么? 感谢您的帮助!

编辑:

new HttpHandler() {
                @Override
                public HttpUriRequest getHttpRequestMethod() {
                    HttpPost httppost = new HttpPost("***********");
                    try {
                        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
                        nameValuePairs.add(new BasicNameValuePair("name", textName.getText().toString()));
                        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    return httppost;
                }
                @Override
                public void onResponse(String result) {
                    try {
                        JSONObject obj = new JSONObject(result);
                        JSONObject response = obj.getJSONObject("response");
                        String requestId = response.getString("request_id");
                        int active = response.getInt("active");

                        SharedPreferences prefs = getApplicationContext().getSharedPreferences("AAAAPrefs", Activity.MODE_PRIVATE);
                        SharedPreferences.Editor edits = prefs.edit();
                        edits.putString("request_id", requestId);
                        edits.putInt("active", active);
                        edits.commit();

                        /* MORE... */

                    } catch (Throwable t) {
                        // Error.
                    }
                }
            }.execute();

【问题讨论】:

  • 它会抛出任何异常还是只是一个空字符串?
  • 遗憾的是我没有得到任何异常或任何东西。只是一个空字符串。
  • 您确定 requestId 不为空吗?上面的代码似乎应该可以工作。你能多展示一点你的代码吗,你到底在哪里设置和获取首选项?
  • 我通过它进行了调试,我设置的 requestId 不为空。我把它放在一个del中。在 HTTP 请求之后。将使用更多代码更新答案。感谢您的帮助!
  • 胡思乱想.. json 解析过程中抛出异常,你永远不会设置字符串。

标签: android sharedpreferences


【解决方案1】:

当我使用以下代码实例化 SharedPreferences 时,它似乎可以工作:

PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

【讨论】:

    【解决方案2】:

    试试这个:

    public static void saveOrEdit(Context context ,String name,
                String key , String value,Class clz){
            if(null == context || StringUtil.isEmpty(name)){
                return;
            }
            SharedPreferences preferences = context.getSharedPreferences(name, Context.MODE_PRIVATE);
            Editor editor = preferences.edit();
            if(null != clz){
                if(clz.getSimpleName().equals("Boolean")){
                    editor.putBoolean(key, Boolean.valueOf(value));
                }else if(clz.getSimpleName().equals("Float")){
                    editor.putFloat(key, Float.valueOf(value));
                }else if(clz.getSimpleName().equals("Integer")){
                    editor.putInt(key, Integer.valueOf(value));
                }else if(clz.getSimpleName().equals("Long")){
                    editor.putLong(key, Long.valueOf(value));
                }else{
                    editor.putString(key, value);
                }
            }else{          
                editor.putString(key, value);
            }
            //It should be submitted
            editor.commit();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-25
      • 1970-01-01
      • 2018-02-08
      • 2016-10-14
      • 2020-04-13
      • 1970-01-01
      相关资源
      最近更新 更多