本文介绍了首选项不能将java.lang.boolean强制转换为String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下Preference类:
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
我在循环中收到此错误我调用setSumary有人能告诉我为什么会这样吗?
I get this error at the loop im calling to setSumary can anyone tell me why this is happening?
更新
文字中的完整stackTrace:
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.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
推荐答案
显然有些prefs被检查,只返回布尔值。
所以你不能直接使用它。
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);
}
这篇关于首选项不能将java.lang.boolean强制转换为String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!