Android共享首选项强制关闭
我试图在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来 mprefs 从未被分配过值(除非它发生在其他地方)
It doesn't look like mprefs is ever assigned a value (unless it's happening somewhere else)
您应该查看日志以查看异常的堆栈爬网,这会告诉您代码崩溃的原因。
我不会将其添加为澄清问题,因为问题中不包含堆栈爬网这一事实强烈表明您还没有真正看过它,如果是这种情况,那么就是您问题的答案最有可能解决您的问题的方法是查看该内容,看看为什么它说您崩溃了。
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.