Android设置中的ListPreference生成ClassCastException,但我似乎没有在代码中找到一个点

时间:2016-08-16 23:08:12

标签: java android xml listpreference

以下是pref_xml.xml文件中Preferences的代码 此文件定义天气应用程序的设置首选项,该应用程序将该位置作为编辑文本首选项和单位作为列表首选项

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<EditTextPreference
    android:title="@string/pref_location_label"
    android:key="@string/pref_location_key"
    android:defaultValue="@string/pref_location_default"
    android:inputType="text"
    android:singleLine="true" />

<ListPreference
    android:title="@string/pref_units_label"
    android:key="@string/pref_units_key"
    android:defaultValue="@string/pref_units_metric"
    android:entryValues="@array/pref_units_values"
    android:entries="@array/pref_units_options"
    />
</PreferenceScreen>

以下是arrays.xml文件的代码,该文件为用户的单位选项定义了pref_units_values和pref_units_options

<resources>

<string-array name="pref_units_options">
    <item>@string/pref_units_label_metric</item>
    <item>@string/pref_units_label_imperial</item>
</string-array>

<string-array name="pref_units_values">
    <item>@string/pref_units_metric</item>
    <item>@string/pref_units_imperial</item>
</string-array>
</resources>

以下是定义文件中所有字符串的代码

<resources>
 <!-- Label for the location preference [CHAR LIMIT=30] -->
    <string name="pref_location_label">Location</string>

    <!-- Key name for storing location in SharedPreferences [CHAR      LIMIT=NONE] -->
    <string name="pref_location_key" translatable="false">location</string>

    <!-- Default postal code for location preference [CHAR LIMIT=NONE] -->
    <string name="pref_location_default" translatable="false">94043</string>
    <!-- Label for the temperature units preference [CHAR LIMIT=30] -->
    <string name="pref_units_label">Temperature Units</string>

    <!-- Label for metric option in temperature unit preference [CHAR LIMIT=25] -->
    <string name="pref_units_label_metric">Metric</string>

    <!-- Label for imperial option in temperature unit preference [CHAR LIMIT=25] -->
    <string name="pref_units_label_imperial">Imperial</string>

    <!-- Key name for temperature unit preference in SharedPreferences [CHAR LIMIT=NONE] -->
    <string name="pref_units_key" translatable="false">units</string>

    <!-- Value in SharedPreferences for metric temperature unit option [CHAR LIMIT=NONE] -->
    <string name="pref_units_metric" translatable="false">metric</string>

    <!-- Value in SharedPreferences for imperial temperature unit option [CHAR LIMIT=NONE] -->
    <string name="pref_units_imperial" translatable="false">imperial</string>
</resources>

接下来是SettingsActivity.java文件的代码,该文件链接pref_general.xml文件中的首选项。

public class SettingsActivity extends PreferenceActivity
  implements Preference.OnPreferenceChangeListener {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Add 'general' preferences, defined in the XML file
    addPreferencesFromResource(R.xml.pref_general);

    // For all preferences, attach an OnPreferenceChangeListener so the UI summary can be
    // updated when the preference changes.
    bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));
    //bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_units_key)));
}

/**
 * Attaches a listener so the summary is always updated with the preference value.
 * Also fires the listener once, to initialize the summary (so it shows up before the value
 * is changed.)
 */
private void bindPreferenceSummaryToValue(Preference preference) {
    // Set the listener to watch for value changes.
    preference.setOnPreferenceChangeListener(this);

    // Trigger the listener immediately with the preference's
    // current value.
    onPreferenceChange(preference,
            PreferenceManager
                    .getDefaultSharedPreferences(preference.getContext())
                    .getString(preference.getKey(), ""));
}

@Override
public boolean onPreferenceChange(Preference preference, Object value) {
    String stringValue = value.toString();

    if (preference instanceof ListPreference) {
        // For list preferences, look up the correct display value in
        // the preference's 'entries' list (since they have separate labels/values).
        ListPreference listPreference = (ListPreference) preference;
        int prefIndex = listPreference.findIndexOfValue(stringValue);
        if (prefIndex >= 0) {
            preference.setSummary(listPreference.getEntries()[prefIndex]);
        }
    } else {
        // For other preferences, set the summary to the value's simple string representation.
        preference.setSummary(stringValue);
    }
    return true;
}

}

假设所有类和包都已导入。当运行上述文件集合以及执行使用首选项值来生成对天气数据库api的http请求的任务的其他活动时,应用程序会在单击菜单中的设置按钮时崩溃。 Logcat条目显示ClassCastException,具有以下详细信息。

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.meet.mysunshine/com.meet.mysunshine.SettingsActivity}: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                 at android.os.Looper.loop(Looper.java:148)
                                                 at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
                                              Caused by: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
                                                 at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:225)
                                                 at android.preference.Preference.getPersistedString(Preference.java:1472)
                                                 at android.preference.ListPreference.onSetInitialValue(ListPreference.java:295)
                                                 at android.preference.Preference.dispatchSetInitialValue(Preference.java:1388)
                                                 at android.preference.Preference.onAttachedToHierarchy(Preference.java:1182)
                                                 at android.preference.PreferenceGroup.addPreference(PreferenceGroup.java:164)
                                                 at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:105)
                                                 at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:104)
                                                 at android.preference.GenericInflater.rInflate(GenericInflater.java:490)
                                                 at android.preference.GenericInflater.inflate(GenericInflater.java:327)
                                                 at android.preference.GenericInflater.inflate(GenericInflater.java:264)
                                                 at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:273)
                                                 at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1521)
                                                 at com.meet.mysunshine.SettingsActivity.onCreate(SettingsActivity.java:38)
                                                 at android.app.Activity.performCreate(Activity.java:6245)
                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                 at android.os.Looper.loop(Looper.java:148) 
                                                 at android.app.ActivityThread.main(ActivityThread.java:5443) 
                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 

PLS。提供帮助查找点生成异常。提前致谢。 并随意询问有关代码的任何其他详细信息。 谢谢!

0 个答案:

没有答案