Android Preference Key关键字问题

今天在Preference.xml中添加Preference时将其中一个key设置为visible_pic,结果在调试时一直报错,错误如下:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.n2hsu.doplus.weibo/com.n2hsu.doplus.weibo.activity.Setting}: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
 at android.app.ActivityThread.access$600(ActivityThread.java:130)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
 at android.os.Handler.dispatchMessage(Handler.java:99)
 at android.os.Looper.loop(Looper.java:137)
 at android.app.ActivityThread.main(ActivityThread.java:4745)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:511)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
 at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
       at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:205)
       at android.preference.Preference.getPersistedString(Preference.java:1406)
       at android.preference.ListPreference.onSetInitialValue(ListPreference.java:278)
 at android.preference.Preference.dispatchSetInitialValue(Preference.java:1322)
 at android.preference.Preference.onAttachedToHierarchy(Preference.java:1134)
 at android.preference.PreferenceGroup.addPreference(PreferenceGroup.java:163)
 at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:104)
 at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:45)
 at android.preference.GenericInflater.rInflate(GenericInflater.java:488)
 at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
 at android.preference.GenericInflater.inflate(GenericInflater.java:326)
 at android.preference.GenericInflater.inflate(GenericInflater.java:263)
 at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:269)
 at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1418)
 at com.n2hsu.doplus.weibo.activity.Setting.onCreate(Setting.java:64)
 at android.app.Activity.performCreate(Activity.java:5008)
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
 ... 11 more

如上所述,说是某处无法将Boolean类型转换为String,但是这个key是写在XML中的,所以错误只能出现在XML硬转码为代码的过程中,其间想通过源码做调试,不成功。在测试时,我在visible前后加个字符都不会出现上述错误。

所以可能的原因有两个:

visible可能在Preference key的转码过程中是关键字;在SharedPreference的XML中visible是关键字。

真实原因还有待查看。