【发布时间】:2012-02-17 23:46:38
【问题描述】:
我刚刚发布了我的第一个安卓动态壁纸。我在我的手机和几个朋友的手机上对其进行了错误测试,没有发现任何问题,但显然在某些设备上它陷入了递归循环并在用户尝试更改设置时导致堆栈溢出错误。
我认为问题正在发生,因为我有某些“主题”设置需要更改其他几个持久值。例如,一个主题将设置默认颜色、速度、背景等。似乎当我使用 Editor.commit() 以编程方式保存这些值时,它会一次又一次地调用 onSharedPreferenceChanged...
由于这是动态壁纸,因此我在透明首选项屏幕后面运行了一个预览,我需要它来反映所做的设置更改。我还需要滑块/颜色选择器/列表首选项来反映用户直接所做的更改,以及在选择“主题”时以编程方式进行的更改。最简单的方法似乎是使用 onSharedPreferenceChanged 中的首选项编辑器更改它们,实际上,这适用于许多设备。
我该怎么做才能让它在所有设备上都能正常工作?
以下是相关代码:
public void onSharedPreferenceChanged(SharedPreferences prefs, String key)
{
if(key != null)
{
SharedPreferences.Editor editor = prefs.edit();
hue = prefs.getInt("color", 0);
BG_COLOR = prefs.getInt("background_color", 0);
//etc...
if(key.matches("plasma_set"))
{
plasmaAtlasName = atlasName;
editor.putString("atlasName", atlasName);
//load each bolt set with defalut values
if(plasmaAtlasName.equals("plasmaAtlas11"))
{
hue = 180;
editor.putInt("speed", 10);
editor.putInt("bolt_density", 2);
BG_COLOR = 0;
editor.putInt("background_color", BG_COLOR);
editor.putInt("color", hue);
}
if(plasmaAtlasName.equals("plasmaAtlas9"))
{
hue = 330;
editor.putInt("speed", 10);
editor.putInt("bolt_density", 2);
BG_COLOR = 0;
editor.putInt("background_color", BG_COLOR);
editor.putInt("color", hue);
}
//etc...
}
editor.commit();
}
}
【问题讨论】:
标签: android recursion overflow sharedpreferences