我正在尝试使用 PreferenceActivity 向我的应用程序添加设置(我的目标是 API 级别 8),我从一个复选框开始:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="pref_fps"
android:title="FPS"
android:defaultValue="false" />
</PreferenceScreen>
我的 PreferenceActivity 子类只定义了一个“onCreate”方法:
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
起初一切似乎都运行良好,但我注意到在应用程序重新启动后首选项丢失了。查看日志,似乎无法将首选项保存到磁盘。每次我单击 PreferenceActivity 中的复选框时,都会出现以下异常:
W/ApplicationContext( 9542): writeFileLocked: Got exception:
W/ApplicationContext( 9542): java.io.UnsupportedEncodingException: utf-8
W/ApplicationContext( 9542): at com.android.internal.util.FastXmlSerializer.setOutput(FastXmlSerializer.java:297)
W/ApplicationContext( 9542): at com.android.internal.util.XmlUtils.writeMapXml(XmlUtils.java:181)
W/ApplicationContext( 9542): at android.app.ContextImpl$SharedPreferencesImpl.writeFileLocked(ContextImpl.java:2928)
W/ApplicationContext( 9542): at android.app.ContextImpl$SharedPreferencesImpl.access$300(ContextImpl.java:2676)
W/ApplicationContext( 9542): at android.app.ContextImpl$SharedPreferencesImpl$EditorImpl.commit(ContextImpl.java:2861)
W/ApplicationContext( 9542): at android.preference.Preference.tryCommit(Preference.java:1198)
W/ApplicationContext( 9542): at android.preference.Preference.persistBoolean(Preference.java:1403)
W/ApplicationContext( 9542): at android.preference.CheckBoxPreference.setChecked(CheckBoxPreference.java:154)
W/ApplicationContext( 9542): at android.preference.CheckBoxPreference.onClick(CheckBoxPreference.java:143)
W/ApplicationContext( 9542): at android.preference.Preference.performClick(Preference.java:810)
W/ApplicationContext( 9542): at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:198)
W/ApplicationContext( 9542): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
W/ApplicationContext( 9542): at android.widget.ListView.performItemClick(ListView.java:3382)
W/ApplicationContext( 9542): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
W/ApplicationContext( 9542): at android.os.Handler.handleCallback(Handler.java:587)
W/ApplicationContext( 9542): at android.os.Handler.dispatchMessage(Handler.java:92)
W/ApplicationContext( 9542): at android.os.Looper.loop(Looper.java:144)
W/ApplicationContext( 9542): at android.app.ActivityThread.main(ActivityThread.java:4937)
W/ApplicationContext( 9542): at java.lang.reflect.Method.invokeNative(Native Method)
W/ApplicationContext( 9542): at java.lang.reflect.Method.invoke(Method.java:521)
W/ApplicationContext( 9542): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
W/ApplicationContext( 9542): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
W/ApplicationContext( 9542): at dalvik.system.NativeStart.main(Native Method)
W/ApplicationContext( 9542): Caused by: java.nio.charset.UnsupportedCharsetException: The unsupported charset name is "utf-8".
W/ApplicationContext( 9542): at java.nio.charset.Charset.forName(Charset.java:544)
W/ApplicationContext( 9542): at com.android.internal.util.FastXmlSerializer.setOutput(FastXmlSerializer.java:292)
W/ApplicationContext( 9542): ... 22 more
模拟器和真实设备上的行为是相同的。
我究竟做错了什么?