Domanda

I am populating shared preferences into a PreferenceFragment, available as a tab in the ActionBar. The preferences are populated in the Fragment's onCreate method,

public static class WIKPreferenceFragment extends PreferenceFragment
{
    @Override
    public void onCreate ( Bundle bundle )
    {
        super.onCreate( bundle );
        this.addPreferencesFromResource( R.layout.settings_fragment );
    }
}

When the tab is selected, the PreferenceFragment is added directly,

public void onTabSelected ( Tab tab, FragmentTransaction transaction )
{
    transaction.replace( R.id.tab_view_target, new WIKPreferenceFragment() );
    ( (WIKActivity) this.activity ).setTabMemory( tab.getPosition() );
}

The content of R.layout.settings_fragment is

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" >
    <PreferenceCategory android:title="@string/category_proximity_settings" >
        <CheckBoxPreference
            android:key="@+key/USE_PASSIVE_GPS"
            android:summary="@string/summary_use_passive_gps"
            android:title="@string/title_use_passive_gps" />
        <EditTextPreference
            android:inputType="number"
            android:key="@+key/GPS_TIMEOUT"
            android:summary="@string/summary_gps_timeout"
            android:title="@string/title_gps_timeout" />
    </PreferenceCategory>
</PreferenceScreen>

With a clean install, the preference fragment appears correctly when the tab is first selected. If another tab is selected and this tab is returned to, the following error occurs:

05-14 19:23:02.366: E/AndroidRuntime(11744): FATAL EXCEPTION: main
05-14 19:23:02.366: E/AndroidRuntime(11744): Process: org.eightbeers.android.wik, PID: 11744
05-14 19:23:02.366: E/AndroidRuntime(11744): java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
05-14 19:23:02.366: E/AndroidRuntime(11744):    at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:224)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at android.preference.Preference.getPersistedString(Preference.java:1429)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at android.preference.EditTextPreference.onSetInitialValue(EditTextPreference.java:154)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at android.preference.Preference.dispatchSetInitialValue(Preference.java:1345)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at android.preference.Preference.onAttachedToHierarchy(Preference.java:1140)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at android.preference.PreferenceGroup.addPreference(PreferenceGroup.java:163)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:104)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:45)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at android.preference.GenericInflater.rInflate(GenericInflater.java:488)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at android.preference.GenericInflater.inflate(GenericInflater.java:326)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at android.preference.GenericInflater.inflate(GenericInflater.java:263)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at android.preference.PreferenceFragment.addPreferencesFromResource(PreferenceFragment.java:285)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at org.eightbeers.android.wik.activity.SettingsTab$WIKPreferenceFragment.onCreate(SettingsTab.java:27)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at android.app.Fragment.performCreate(Fragment.java:1688)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:860)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1063)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at android.app.BackStackRecord.run(BackStackRecord.java:684)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1450)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:444)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at android.os.Handler.handleCallback(Handler.java:733)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at android.os.Handler.dispatchMessage(Handler.java:95)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at android.os.Looper.loop(Looper.java:136)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at android.app.ActivityThread.main(ActivityThread.java:5105)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at java.lang.reflect.Method.invokeNative(Native Method)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at java.lang.reflect.Method.invoke(Method.java:515)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
05-14 19:23:02.366: E/AndroidRuntime(11744):    at dalvik.system.NativeStart.main(Native Method)

Some potentially-helpful data points:

  • This error does not occur when the CheckBoxPreference is commented-out, or if the EditTextPreference is commented-out (i.e., everything works if there is only one preference; of course, I'd like to have more than one, more than two, even)

  • This error does not occur if preferences are not reloaded in onCreate if they've already been loaded; however, when this is changed no preferences appear at all when the tab is reselected

  • This error persists through clearing of user data, restarting Eclipse, uninstalling of the app, project cleans, and phone reboots (okay, just one); with exception to the phone reboot (since I only tried it once), it persists through any combination of the previous

  • To make uninstalls easier, I renamed the app from WIK to AAWIK; however, in the Settings -> Application Manager window the app still appears alphabetized as WIK. Everywhere else in the UI --- app drawer, uninstall pane, notifications, etc. --- it is AAWIK; this might just be an extreme caching issue? (Edit: this last one was addressed by just letting the phone sit, idle, long enough for me to type this question; no longer a useful data point, all other issues still persist.)

È stato utile?

Soluzione

The problem was solved by switching the android:key="@+key/[keyname]" in the XML for android:key="[keyname]".

I do not intend to accept my answer immediately since I have no idea of why this worked, and Google has not been forthcoming.

Altri suggerimenti

Key should be stored as string in prefs.xml (or another name if you want)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="pref_key_gps" translatable="false">pref_key_gps</string>
</resources>

you should replace:

android:key="@+key/USE_PASSIVE_GPS"

to:

android:key="@string/pref_key_gps"

Chinese

现在碰到的问题是,addPreferencesFromResource ClassCastException Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

我所使用到的控件 <EditTextPreference .../>

我的业务场景:

  1. 通过 EditTextPreference 处理的数据都是数字。
  2. 首次将数据写入 xml 文件的工作不是通过 EditTextPreference 完成的;我是手动完成的,使用的方法是 putInt
  3. 在加载包含 EditTextPreference 布局的时候报错。

错误原因,在 EditTextPreference 类内部所维护的数据是通过 putStringgetString 来维护的。 所以我只需要将手动部分设置成 putString 即可解决问题。

English

My problem is : addPreferencesFromResource ClassCastException Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

The widget I am using is: <EditTextPreference .../>

My business scene is:

  1. EditTextPreference only contrue numbers.
  2. I write data to xml file by SharedPerference.Editro.putInt() ,not done by `EditTextPreference' ,in the first time .
  3. App creash when load EditTextPreference layout.

The cause of the problem is :

  • Inside the EditTextPreference class , data maintained by putString and getString .But I used is putInt
  • So change putInt to putString can fix the problem
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top