Android共享首选项强制关闭

发布于 2024-11-17 18:22:23 字数 1255 浏览 1 评论 0原文

我试图在 PreferenceActivity 中使用共享首选项,但不幸的是它强制关闭。部分内容:

public class EditPreferences extends PreferenceActivity {

String ListPreference;
boolean CheckboxPreference;
SharedPreferences mprefs;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);


final CheckBoxPreference checkboxPref = (CheckBoxPreference) getPreferenceManager().findPreference("checkboxPref");

checkboxPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {            
public boolean onPreferenceChange(Preference preference, Object newValue) {
    if (newValue.toString().equals("true"))
    {  
        Toast.makeText(getApplicationContext(), "CB: " + "true", Toast.LENGTH_SHORT).show();
        SharedPreferences.Editor edit = mprefs.edit();
        edit.putString("cbstate", "true");
        edit.commit();
    } 
    else 
    {  
        Toast.makeText(getApplicationContext(), "CB: " + "false", Toast.LENGTH_SHORT).show();
    SharedPreferences.Editor edit = mprefs.edit(); //this line force closes
        edit.putString("cbstate", "false");
        edit.commit();
    }
    return true;
}

});

代码有什么问题吗? 谢谢, 乙

I am trying to use sharedpreferences in a PreferenceActivity, but unfortunately it force closes. Part of it:

public class EditPreferences extends PreferenceActivity {

String ListPreference;
boolean CheckboxPreference;
SharedPreferences mprefs;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);


final CheckBoxPreference checkboxPref = (CheckBoxPreference) getPreferenceManager().findPreference("checkboxPref");

checkboxPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {            
public boolean onPreferenceChange(Preference preference, Object newValue) {
    if (newValue.toString().equals("true"))
    {  
        Toast.makeText(getApplicationContext(), "CB: " + "true", Toast.LENGTH_SHORT).show();
        SharedPreferences.Editor edit = mprefs.edit();
        edit.putString("cbstate", "true");
        edit.commit();
    } 
    else 
    {  
        Toast.makeText(getApplicationContext(), "CB: " + "false", Toast.LENGTH_SHORT).show();
    SharedPreferences.Editor edit = mprefs.edit(); //this line force closes
        edit.putString("cbstate", "false");
        edit.commit();
    }
    return true;
}

});

What is wrong with the code?
Thanks,
B

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

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

发布评论

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

评论(2

倚栏听风 2024-11-24 18:22:23

看起来 mprefs 从未被分配过值(除非它发生在其他地方)

It doesn't look like mprefs is ever assigned a value (unless it's happening somewhere else)

番薯 2024-11-24 18:22:23

您应该查看日志以查看异常的堆栈爬网,这会告诉您代码崩溃的原因。

我不会将其添加为澄清问题,因为问题中不包含堆栈爬网这一事实强烈表明您还没有真正看过它,如果是这种情况,那么就是您问题的答案最有可能解决您的问题的方法是查看该内容,看看为什么它说您崩溃了。

You should look at the log to see the stack crawl of the exception, which tells you why your code is crashing.

I am not adding this as a question for clarification, because the fact that a stack crawl is not included in the question is a strong indication that you haven't actually looked at it, and if that is the case then the answer to your question and most likely solution to your problem is to go look at that and see why it says you are crashing.

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