安卓==>偏爱?

发布于 2024-09-06 17:25:54 字数 476 浏览 10 评论 0原文

我的应用程序因以下代码中的空指针异常而崩溃。 我在 res/xml/defaults.xml 下有一个 xml 首选项文件 知道为什么会崩溃吗?

public class Preference extends Activity {
    public Preference()
    {
    }

    public String getPreference(String key)
    {
                //it still crashes here
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
        String result = settings.getString(key,  null);
        return result;
    }
}

my app crashes with a null pointer exception on the code below.
i have an xml preference file under res/xml/defaults.xml
Any idea why it's crashing?

public class Preference extends Activity {
    public Preference()
    {
    }

    public String getPreference(String key)
    {
                //it still crashes here
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
        String result = settings.getString(key,  null);
        return result;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

桃扇骨 2024-09-13 17:25:54

首选项文件存储在项目的/res/xml/defaults.xml中,

它们存储在设备上您的应用程序文件夹中,例如

/data/data/com.your.pkg/default.prefs

尝试不要指定文件名,如你会遇到一些首选项文件的问题,就像这个OP有 这里

SharedPreferences preferences = PreferenceManager
                    .getDefaultSharedPreferences(context);

那么你可能需要查询

preferences.getString('weightPref', null);

Preference files are not storead in project's /res/xml/defaults.xml

They are stored on the device in your application folder something like

/data/data/com.your.pkg/default.prefs

Try do not specify the file name, as you will have some problems with the preference files, like this OP had here

SharedPreferences preferences = PreferenceManager
                    .getDefaultSharedPreferences(context);

Then you will probably have to query

preferences.getString('weightPref', null);
独自唱情﹋歌 2024-09-13 17:25:54

这是一个示例代码,展示了如何保存和检索首选项。在这里,我将用户名和密码保存在 SharedPreferences 中。

SharedPreferences uPreferences = getSharedPreferences("CurrentUser", MODE_PRIVATE);
SharedPreferences.Editor editor; = uPreferences.edit(); //Instantiating editor object


protected void storeSharedPrefs(String username, String password) {
        /*
         * Storing in Shared Preferences
         */
        editor.putString("username", username);
        editor.putString("password", password);     
        editor.commit();  //Commiting changes
    }

从 SharedPreferences 中检索另一个活动中的用户名和密码。

 private SharedPreferences mSP;
    mSP = getSharedPreferences("CurrentUser", MODE_PRIVATE);
            String username = mSP.getString("username", null);
            String password = mSP.getString("password", null);

希望有帮助..

Here's a sample code which shows how to save and retrieve Preferences. Here I am saving username and password in SharedPreferences.

SharedPreferences uPreferences = getSharedPreferences("CurrentUser", MODE_PRIVATE);
SharedPreferences.Editor editor; = uPreferences.edit(); //Instantiating editor object


protected void storeSharedPrefs(String username, String password) {
        /*
         * Storing in Shared Preferences
         */
        editor.putString("username", username);
        editor.putString("password", password);     
        editor.commit();  //Commiting changes
    }

Retrieving username and password in another activity from SharedPreferences.

 private SharedPreferences mSP;
    mSP = getSharedPreferences("CurrentUser", MODE_PRIVATE);
            String username = mSP.getString("username", null);
            String password = mSP.getString("password", null);

Hope it helps..

毅然前行 2024-09-13 17:25:54

在共享首选项中设置值:

Editor prefs = getSharedPreferences("Application_name", MODE_PRIVATE).edit();
prefs.putString("key", accountKey);
prefs.commit();

从另一个活动获取值:

String accountKey = 
    this.getSharedPreferences("Application_name", MODE_PRIVATE).
    getString("key", null);

如果您使用某些预定义的处理程序(例如 getString(R.string._key))而不是访问该变量,那就太好了硬编码字符串“key”

Setting a value in the shared preferences:

Editor prefs = getSharedPreferences("Application_name", MODE_PRIVATE).edit();
prefs.putString("key", accountKey);
prefs.commit();

Getting the value from another activity:

String accountKey = 
    this.getSharedPreferences("Application_name", MODE_PRIVATE).
    getString("key", null);

It would be nice if you access the variable by using some predefined handler, such as getString(R.string._key), instead of the hardcoded string "key".

兔小萌 2024-09-13 17:25:54

您的首选项应扩展 PreferenceActivity。然后,您需要为首选项创建一个资源 xml 文件,并在 PreferenceActivity 中引用该文件,如下所示:

@Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
            etc.
    }

首选项 xml 应该有一个 PreferenceScreen 作为顶级元素,并且您可以利用 Android 提供的所有不同的首选项视图用于设置首选项。这将是最常见、最优雅的方法。

Your Preferences should extend PreferenceActivity. Then you need to create a resource xml file for preferences, and reference that in your PreferenceActivity like so:

@Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
            etc.
    }

The preferences xml should have a PreferenceScreen as the top level element, and you can take advantage of all the different preference views Android makes available to you for setting preferences. This would be the most common, and elegant way to do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文