共享preference Android的数据存储 [英] Shared Preference Android Storing data

查看:143
本文介绍了共享preference Android的数据存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的共享preference有麻烦存储数据。
如果我有以下的code和尝试运行它,它崩溃。我不知道为什么,虽然。

I am having trouble storing data using shared preference. If I have the following code and try to run it, it crashes. I don't know why though.

public class Favorites extends Activity{

    private static final String TAG_NAME = "title";
    private static final String TAG_URL = "href";


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.favorites);

        Intent in = getIntent();
        TextView favName = (TextView) findViewById(R.id.textView1);

        String FILENAME = "settings";
        String string = "hello world!";

        SharedPreferences pref = getSharedPreferences("Preference",
                MODE_WORLD_READABLE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putBoolean("keyBoolean", true);
        editor.putFloat("keyFloat", 1.0f);
        editor.putInt("keyInt", 1);
        editor.putLong("keyLong", 1000000L);
        editor.putString("keyString", "Hello Android");
        editor.commit();

        boolean dataFromPrefBool = pref.getBoolean("keyBoolean", false);
        float dataFromPrefflaot = pref.getFloat("keyFloat", 0.0f);
        int dataFromPrefInt = pref.getInt("keyInt", 0);
        long dataFromPrefLong = pref.getLong("keyLong", 0);
        String dataFromPrefString = pref.getString("keyString", null);

        favName.setText(dataFromPrefInt);
}

为什么没有任何反应?这些只是虚拟值,但仍然没有任何反应。

Why is nothing happening? These are just dummy values but still nothing happens

推荐答案

试试这个,也许是由于空指针exception.I我不知道。

Try this, maybe it is due to null pointer exception.I am not sure.

public class Favorites extends Activity{

    private static final String TAG_NAME = "title";
    private static final String TAG_URL = "href";


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.favorites);

        Intent in = getIntent();
        TextView favName = (TextView) findViewById(R.id.textView1);

        String FILENAME = "settings";
        String string = "hello world!";

        SharedPreferences pref = getSharedPreferences("Preference",
                MODE_WORLD_READABLE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putBoolean("keyBoolean", true);
        editor.putFloat("keyFloat", 1.0f);
        editor.putInt("keyInt", 1);
        editor.putLong("keyLong", 1000000L);
        editor.putString("keyString", "Hello Android");
        editor.commit();

        boolean dataFromPrefBool = pref.getBoolean("keyBoolean", null);
        float dataFromPrefflaot = pref.getFloat("keyFloat", null);
        int dataFromPrefInt = pref.getInt("keyInt", null);
        long dataFromPrefLong = pref.getLong("keyLong", null);
        String dataFromPrefString = pref.getString("keyString", null);

        if(dataFromPrefInt==null)
        {
            favName.setText("");
        }
        else
        {
            favName.setText(dataFromPrefInt);
        }
    }
}

这篇关于共享preference Android的数据存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆