写背景颜色

时间:2014-07-27 19:05:43

标签: java android sharedpreferences

我正在尝试从三个SeekBars中获取背景颜色。 我使用这段代码:

SharedPreferences mColor;
public static final String APP_PREFERENCES_COLOR="color";
    colorS = rgbToHEX(r, g, b);
    try {

                Editor editor = mColor.edit();
                editor.putString(APP_PREFERENCES_COLOR, colorS);
                editor.apply();
                } catch(Exception e){
                 Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
                }
    }

但我看到消息“java.lang.NullPointerExeption” 我该如何解决?

1 个答案:

答案 0 :(得分:0)

获得编辑器时,

SharedPreferences为null。尝试:

mColor = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Editor editor = mColor.edit();
editor.putString(APP_PREFERENCES_COLOR, colorS);
editor.commit();

应该解决你的问题。

希望它有所帮助。