首选项不能将java.lang.boolean强制转换为String

时间:2013-05-02 02:29:21

标签: java android sharedpreferences

我有以下Preference类:

public class AppPreferencesActivity extends PreferenceActivity {
    private SharedPreferences appPrefs;
    private SharedPreferences.Editor prefEditor;
    private Mediator mediator;
    private SharedPreferences.OnSharedPreferenceChangeListener listener;
    private User user;
    @SuppressWarnings("deprecation")
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //---load the preferences from an XML file---
        addPreferencesFromResource(R.xml.preferences);
        this.appPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        this.mediator = (Mediator) getApplication();    
        this.prefEditor = appPrefs.edit();
        this.user = mediator.getUser();
        setPreferences();
        listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
            public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
                // Implementation
                updatePreference(key);
                Toast.makeText(AppPreferencesActivity.this,
                        "Information updated",
                        Toast.LENGTH_SHORT).show();
            }
        };
        appPrefs.registerOnSharedPreferenceChangeListener(listener);

    } 
    private void setPreferences(){
        prefEditor.putString("nameEdit", user.getName() + " "+user.getLastName());
        prefEditor.putString("streetEdit", user.getStreetName());
        prefEditor.putString("streetNumberEdit", ""+user.getStreetNr());
        prefEditor.putString("emailEdit", user.getEmail());
        prefEditor.commit();
        preferenceAddSummary();
    }
    public boolean checkEmailStatus(){
        return appPrefs.contains("emailEdit");
    }
    @SuppressWarnings("deprecation")
    private void preferenceAddSummary() {
        for (Entry<String, ?> key : appPrefs.getAll().entrySet()) {
            findPreference(key.getKey()).setSummary(appPrefs.getString(key.getKey(), "Not yet entered")); 
// this is where im getting the error

            }

然而,当我运行这个时,我得到以下

  

无法将Java.lang.boolean强制转换为Java.lang.String

我在循环中得到这个错误我调用setSumary有人能告诉我为什么会这样吗?

更新

文本中的完整stackTrace:

   05-02 02:28:15.835: E/AndroidRuntime(1775): FATAL EXCEPTION: main
05-02 02:28:15.835: E/AndroidRuntime(1775): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.konkurrencesigner/com.example.konkurrencesigner.AppPreferencesActivity}: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
05-02 02:28:15.835: E/AndroidRuntime(1775):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-02 02:28:15.835: E/AndroidRuntime(1775):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-02 02:28:15.835: E/AndroidRuntime(1775):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-02 02:28:15.835: E/AndroidRuntime(1775):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-02 02:28:15.835: E/AndroidRuntime(1775):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 02:28:15.835: E/AndroidRuntime(1775):     at android.os.Looper.loop(Looper.java:137)
05-02 02:28:15.835: E/AndroidRuntime(1775):     at android.app.ActivityThread.main(ActivityThread.java:5039)
05-02 02:28:15.835: E/AndroidRuntime(1775):     at java.lang.reflect.Method.invokeNative(Native Method)
05-02 02:28:15.835: E/AndroidRuntime(1775):     at java.lang.reflect.Method.invoke(Method.java:511)
05-02 02:28:15.835: E/AndroidRuntime(1775):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-02 02:28:15.835: E/AndroidRuntime(1775):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-02 02:28:15.835: E/AndroidRuntime(1775):     at dalvik.system.NativeStart.main(Native Method)
05-02 02:28:15.835: E/AndroidRuntime(1775): Caused by: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
05-02 02:28:15.835: E/AndroidRuntime(1775):     at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:224)
05-02 02:28:15.835: E/AndroidRuntime(1775):     at com.example.konkurrencesigner.AppPreferencesActivity.preferenceAddSummary(AppPreferencesActivity.java:55)
05-02 02:28:15.835: E/AndroidRuntime(1775):     at com.example.konkurrencesigner.AppPreferencesActivity.setPreferences(AppPreferencesActivity.java:47)
05-02 02:28:15.835: E/AndroidRuntime(1775):     at com.example.konkurrencesigner.AppPreferencesActivity.onCreate(AppPreferencesActivity.java:28)
05-02 02:28:15.835: E/AndroidRuntime(1775):     at android.app.Activity.performCreate(Activity.java:5104)
05-02 02:28:15.835: E/AndroidRuntime(1775):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-02 02:28:15.835: E/AndroidRuntime(1775):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-02 02:28:15.835: E/AndroidRuntime(1775):     ... 11 more

4 个答案:

答案 0 :(得分:18)

我得到了同样的错误。我去了并删除了应用程序的数据,我的应用程序不再崩溃。

答案 1 :(得分:6)

显然,检查了一些prefs并且只返回boolean值。 所以你不能直接使用它。

for (Entry<String, ?> key : appPrefs.getAll().entrySet()) {
    findPreference(key.getKey()).setSummary(appPrefs.getString(key.getKey(), "Not yet entered"));
}

我想也许你应该这样使用:

for (Entry<String, ?> key : appPrefs.getAll().entrySet()) {
    Object result = key.getValue();
    if (result instanceof Boolean) {
        //handle boolean
    } else if (result instanceof String) {
        //handle String
    }
    findPreference(key.getKey()).setSummary(result);
}

答案 2 :(得分:5)

我也有这个错误。当我第一次有一个复选框首选项,然后使用相同的名称作为列表首选项时,它很高兴,但在我的手机中,它仍然保存为布尔值,并尝试将其加载到我的列表中。

修复:清除应用数据

答案 3 :(得分:0)

我也遇到了这个错误,无法弄清楚原因。我想这是因为我添加了一个新的偏好并尝试再次运行该应用程序。我卸载了应用程序,然后使用新的首选项再次运行它,它工作正常。也许它需要一个完整的擦除来重置sharedpreferences文件。