我正在尝试创建一个 PreferenceActivity 并使用 xml 文件来加载可由用户配置的首选项。但不知何故,“默认值”没有正确加载。这是我的preferenceActivity代码:
public class Instellingen extends PreferenceActivity{
public static final int mode= Activity.MODE_PRIVATE;
public static final String KEY_LOCKACTIONS = "lockactions";
public static final String KEY_VERSIE = "Versie";
public static final String KEY_CHAUFFEUR = "Chauffeur";
public static final String KEY_LATITUDE = "Latitude";
public static final String KEY_LONGITUDE = "Longitude";
public static final String KEY_NEEDLOCUPDATE = "NeedLocUpdate";
public static final String KEY_NEEDAPPUPDATE = "NeedAppUpdate";
public static final String KEY_NEEDSYNC = "NeedSync";
public static final String KEY_SYNCCONTENT = "NeedSyncContent";
public static final String KEY_THREADCOUNT = "ThreadCount";
public static final String KEY_FREESYNCTHREAD = "FreeSyncThread";
public static final long version = 0;
static SharedPreferences mySharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
}
public static Boolean GetBoolean(Context context, String Name)
{
mySharedPreferences = context.getSharedPreferences("KCStandaardSettings", mode);
return mySharedPreferences.getBoolean(Name, false);
}
这是我试图获取价值的一段代码:
@Override
public void run() {
Boolean music = Instellingen.GetBoolean(contextAfterThread, "noteSound");
for (int i = 0; i < number; i++) {
try {
sleep(500);
} catch (InterruptedException e) {
TLogFile.appendLog("e", "log_tag", "Error sleeping soundthread " + e.toString());
e.printStackTrace();
}
if(music){
TSound.PlayNotification(contextAfterThread);
}
}
}
这是xml本身(文件名是preferences.xml)
<CheckBoxPreference
android:id="@+id/cbRemainLoggedPreferences"
android:defaultValue="false"
android:key="RemainLoggedIn"
android:title="@string/remainLoggedIn" />
<CheckBoxPreference
android:id="@+id/cbNotificationSound"
android:defaultValue="true"
android:key="noteSound"
android:title="@string/Sound" />
<!-- <CheckBoxPreference
android:id="@+id/cbWidgetAllPreferences"
android:defaultValue="true"
android:key="cbWidgetAll"
android:title="Toon in de Widget de tab met alle berichten" />
<CheckBoxPreference
android:id="@+id/cbWidgetUnreadPreferences"
android:defaultValue="true"
android:key="cbWidgetUnread"
android:title="Toon in de Widget de tab met ongelezen berichten" />
<CheckBoxPreference
android:id="@+id/cbWidgetReadPreferences"
android:defaultValue="true"
android:key="cbWidgetRead"
android:title="Toon in de Widget de tab met gelezen berichten" />-->
我知道之前有很多关于同一问题的问题。我仍然问这个的原因是因为它们都不适合我。我试过使用: PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
但就像我说的那样,它并没有解决我的问题。需要明确的是,问题在于未找到导致 SharedPreferences 函数自身返回的默认“错误”值的密钥。
由于我使用targetsdk lvl 8构建此应用程序,是否有可能发生此问题?这是安卓2.2