所以可能是另一个新的问题:)
我有一个Activity,一个appwidgetprovider和一个PreferenceActivity.在PreferenceActivity中我有一个ListPreference,我用它来定义小部件的文本大小.主活动启动时,sharedPreferences加载时没有任何问题,但是一旦运行PreferenceActivity,我就无法再在主活动中正确检索sharedPreference值.我尝试直接在PreferenceActivity中检索sharedPreference值并发生同样的事情:出于某种原因,当我尝试从sharedPreferences获取值时,我调用addPreferencesFromresource(R.drawable.settings); (在评论’测试1′),它说:
“onStart() 1, size: small” and “Small works!”
即有用.但是当我尝试在资源调用之后检索值时(在评论’测试2′),它说:
“onStart() 2, size: small” and “It doesn’t work …”
即它不起作用.我不知道为什么.显然它获得了sharedPreference的值,因为它表示当前的textWidgetSize在两种情况下都等于“small”,但由于某种原因,它在资源调用之后并不认为“small”=“small”.你们有什么想法吗?
这是PreferenceActivity代码:
package dk.mfoller.androID.basicnote;import androID.content.SharedPreferences;import androID.os.Bundle;import androID.preference.PreferenceActivity;import androID.preference.PreferenceManager;import androID.Widget.Toast;import dk.mfoller.androID.basicnote.R;public class BasicNoteSettings extends PreferenceActivity{ String WidgetTextSize = "small"; boolean WidgetlineCounter = true; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Calls a function to get the preferences getPrefs(); // Test 1 maketoast("onStart() 1, size: " + WidgetTextSize); if(WidgetTextSize == "small") { maketoast("Small works!"); } else if(WidgetTextSize == "medium") { maketoast("Medium works!"); } else if(WidgetTextSize == "large") { maketoast("Large works!"); } else { maketoast("It doesn't work ..."); } // Gets the preference layout from xml addPreferencesFromresource(R.drawable.settings); // Calls a function to get the preferences getPrefs(); // Test 2 maketoast("onStart() 2, size: " + WidgetTextSize); if(WidgetTextSize == "small") { maketoast("Small works!"); } else if(WidgetTextSize == "medium") { maketoast("Medium works!"); } else if(WidgetTextSize == "large") { maketoast("Large works!"); } else { maketoast("It doesn't work ..."); } } // A function to get the preferences private voID getPrefs() { // Gets data from the shared preferences SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); WidgetTextSize = prefs.getString("text_size_List", "small"); WidgetlineCounter = prefs.getBoolean("line_counter_cBox", true); } // A function to display a popup private voID maketoast(String popup) { Toast.makeText(this, popup, Toast.LENGTH_SHORT).show(); }}
这是xml文档:
<?xml version="1.0" enCoding="utf-8"?><PreferenceScreen xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:key="basicnote_settings" androID:title="@string/settings_label"> <Preferencecategory androID:title="Text size"> <ListPreference androID:key="text_size_List" androID:title="Widget text size" androID:summary="@string/text_size_summary" androID:entrIEs="@array/text_size_options" androID:entryValues="@array/text_size_values" androID:defaultValue="small" /> </Preferencecategory> <Preferencecategory androID:title="Other settings"> <CheckBoxPreference androID:key="line_counter_cBox" androID:title="line counter" androID:summary="@string/hIDden_lines_summary" androID:defaultValue="true" /> </Preferencecategory></PreferenceScreen>
这是我的资源字符串:
<?xml version="1.0" enCoding="utf-8"?><!-- defines varIoUs resources --><resources> <string name="app_name">basicNote</string> <string name="note_hint">Tap to add some notes ...</string> <string name="fake_load">Loading notes ...</string> <string name="settings_label">basicNote settings</string> <string name="text_size_summary">Set the size of the Widget text</string> <string-array name="text_size_options"> <item>Small (12sp)</item> <item>Medium (13sp)</item> <item>Large (15sp)</item> </string-array> <string-array name="text_size_values"> <item>small</item> <item>medium</item> <item>large</item> </string-array> <string name="hIDden_lines_summary">Show/hIDe the number of lines not shown in the Widget</string></resources>
解决方法:
您在String对象上使用==.请改用.equals(yourString).
总结以上是内存溢出为你收集整理的android – addPreferencesFromResource使sharedPreferences不起作用全部内容,希望文章能够帮你解决android – addPreferencesFromResource使sharedPreferences不起作用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)