1

我正在使用 holoeverywhere 库,当我启动 ActivityPreference 时出现此错误:

06-22 22:54:23.425: E/AndroidRuntime(11853): FATAL EXCEPTION: main
06-22 22:54:23.425: E/AndroidRuntime(11853): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tufan.liveprices/com.tufan.liveprices.ActivityPreferences}: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
06-22 22:54:23.425: E/AndroidRuntime(11853):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at android.os.Looper.loop(Looper.java:137)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at android.app.ActivityThread.main(ActivityThread.java:4898)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at java.lang.reflect.Method.invokeNative(Native Method)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at java.lang.reflect.Method.invoke(Method.java:511)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at dalvik.system.NativeStart.main(Native Method)
06-22 22:54:23.425: E/AndroidRuntime(11853): Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
06-22 22:54:23.425: E/AndroidRuntime(11853):    at android.app.SharedPreferencesImpl.getInt(SharedPreferencesImpl.java:221)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at org.holoeverywhere.preference._SharedPreferencesImpl_XML.getInt(_SharedPreferencesImpl_XML.java:232)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at org.holoeverywhere.preference._SharedPreferencesImpl_XML.getInt(_SharedPreferencesImpl_XML.java:232)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at org.holoeverywhere.preference.Preference.getPersistedInt(Preference.java:332)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at org.holoeverywhere.preference.NumberPickerPreference.onSetInitialValue(NumberPickerPreference.java:190)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at org.holoeverywhere.preference.Preference.dispatchSetInitialValue(Preference.java:202)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at org.holoeverywhere.preference.Preference.onAttachedToHierarchy(Preference.java:492)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at org.holoeverywhere.preference.PreferenceGroup.addPreference(PreferenceGroup.java:74)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at org.holoeverywhere.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:40)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at org.holoeverywhere.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:1)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at org.holoeverywhere.internal.GenericInflater.rInflate(GenericInflater.java:262)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at org.holoeverywhere.internal.GenericInflater.rInflate(GenericInflater.java:263)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at org.holoeverywhere.internal.GenericInflater.inflate(GenericInflater.java:194)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at org.holoeverywhere.internal.GenericInflater.inflate(GenericInflater.java:164)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at org.holoeverywhere.internal.GenericInflater.inflate(GenericInflater.java:158)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at org.holoeverywhere.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:380)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at org.holoeverywhere.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:282)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at com.tufan.liveprices.ActivityPreferences.onCreate(ActivityPreferences.java:15)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at android.app.Activity.performCreate(Activity.java:5206)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
06-22 22:54:23.425: E/AndroidRuntime(11853):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
06-22 22:54:23.425: E/AndroidRuntime(11853):    ... 11 more

偏好活动:

package com.***;

import org.holoeverywhere.preference.PreferenceActivity;
import android.os.Bundle;

public class ActivityPreferences extends PreferenceActivity {
    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.activity_preferences);
    }
}

activity_preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:holo="http://schemas.android.com/apk/res-auto">

    <PreferenceCategory
        holo:key="cat_refresh"
        holo:title="@string/preferencecategory_refresh" >

        <CheckBoxPreference
            holo:key="autoRefresh"
            holo:title="@string/preference_autorefreshtitle"
            holo:summary="@string/preference_autorefreshsummary"
            holo:defaultValue="true" />

        <org.holoeverywhere.preference.NumberPickerPreference
            holo:key="refreshInterval"
            holo:title="@string/preference_refreshintervaltitle"
            holo:summary="@string/preference_refreshintervalsummary"
            holo:defaultValue="5"
            holo:max="200"
            holo:min="1" />

    </PreferenceCategory>
</PreferenceScreen>

Holoeverywhere 版本: 1.6.1
Android SDK: 4.2
测试于: Android 4.1.2 (gt-i9300)

问题出在哪里?我在哪里犯错?

4

1 回答 1

2

我之前使用过EditTextPreference。因此refreshInterval键用于字符串值。我从手机上卸载了应用程序并重新安装了它。所以偏好值被清理了。

现在用于整数和应用程序的 refreshInterval 键工作正常。

于 2013-06-24T10:25:00.190 回答