开发者

Android sharedpreferences force closes

开发者 https://www.devze.com 2023-03-15 22:40 出处:网络
I am trying to use sharedpreferences in a PreferenceActivity, but unfortunately it force closes. Part of it:

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"))
    {  
开发者_如何学Python        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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消