onSharedPreferenceChanged 在某些 Android 设备上导致堆栈溢出
我刚刚发布了我的第一个 Android 动态壁纸。我在我的手机和几个朋友的手机上对其进行了错误测试,没有发现任何问题,但显然在某些设备上,它陷入了递归循环,并在用户尝试更改设置时导致堆栈溢出错误。
我相信出现问题是因为我有某些“主题”设置,需要更改其他几个持久值。例如,一个主题将设置默认颜色、速度、背景等。似乎当我使用 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();
}
}
I've just released my first live wallpaper for android. I bug tested it on my phone and several friends phones without finding any problems, but apparently on some devices it's getting stuck in a recursive loop and causing a stack overflow error when the user tries to change settings.
I believe the problem is occurring because I have certain "theme" settings which need to change several other persisted values. For example one theme will set a default color, speed, background, etc. It seems that when I persist these values programmatically with Editor.commit(), it's calling onSharedPreferenceChanged again, and again, and again...
Since this is a live wallpaper, I have a preview running behind the transparent preference screen, and I need it to reflect the settings changes as they're made. I also need the sliders/color pickers/list preferences to reflect changes made both by the user directly, and programmatically when a "theme" is selected. The easiest way to do this seemed to be to change them with a preference editor in onSharedPreferenceChanged, and indeed, this works on many devices.
What can I do to make it work on all devices?
Here's the relevant code:
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我明白了。在调用 Editor.commit() 之前取消注册侦听器,然后再次注册它是一个简单的问题。
Ok, I figured it out. It was a simple matter of unregistering the listener before calling Editor.commit() and then registering it again afterward.