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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16329426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:39:54  来源:igfitidea点击:

Preference cannot cast java.lang.boolean to String

javaandroidsharedpreferences

提问by Marc Rasmussen

I have the following Preference class:

我有以下偏好类:

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

            }

However when i run this i get the following

但是,当我运行它时,我得到以下信息

Cannot cast Java.lang.boolean to Java.lang.String

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

I get this error at the loop im calling to setSumary can anyone tell me why this is happening?

我在调用 setSumary 的循环中收到此错误 谁能告诉我为什么会这样?

UPDATE

更新

Full stackTrace in text:

文本中的完整堆栈跟踪:

   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.access0(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

回答by Svetoslav Marinov

I was getting the same error. I went and deleted App's data and my app doesn't crash anymore.

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

回答by Renzo Ligthart

I had this error too. It happined when i first had a checkbox preference, then used the same name for a list preference, but on my phone in the preferences it was still saved as a boolean and it tried to load that into my list.

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

fix: clear app data

修复:清除应用数据

回答by buptcoder

It obviously that some prefs are checked and only return boolean value. So you cannot use this directly.

很明显,一些首选项被检查并且只返回布尔值。所以你不能直接使用它。

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

I think maybe you should use like that:

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

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);
}

回答by zafirk

I was getting this error too, and couldn't figure out why. I think it's because I added a new preference and tried running the app again. I uninstalled the app, then ran it again with the new preferences, and it worked. Maybe it needed a full wipe to reset the sharedpreferences file.

我也遇到了这个错误,不知道为什么。我认为这是因为我添加了一个新首选项并尝试再次运行该应用程序。我卸载了该应用程序,然后使用新的首选项再次运行它,并且成功了。也许它需要完全擦除才能重置 sharedpreferences 文件。