我使用一個活動驗證碼的getString總是返回默認值一個價值
protected void onResume() {
super.onResume();
prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
System.out.println(connections);
String dir = prefs.getString("download_directory", "Download");
System.out.println(dir);
}
我的preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="Downloads">
<EditTextPreference
android:key="download_directory"
android:title="@string/download_destination"
android:summary="@string/download_description"
android:defaultValue="FacebookPhotos"
android:dialogTitle="@string/download_destination" />
<CheckBoxPreference android:key="subdir_album"
android:title="@string/download_album"
android:summary="@string/download_album_description" />
</PreferenceCategory>
<PreferenceCategory android:title="Gallery">
<EditTextPreference
android:key="connections"
android:title="Concurrent connections"
android:numeric="integer"
android:maxLength="2"
android:defaultValue="7"
android:dialogTitle="Concurrent connections" />
</PreferenceCategory>
</PreferenceScreen>
通過
public class Preferences extends PreferenceActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
的編輯問題是,當我得到「連接」的價值,它給了我正確的int,而不是關鍵字「download_directory」alwa的getString YS給我的默認值「下載」
您可以在設置值時發佈代碼嗎? – zabawaba99
您的preferences.xml的其餘部分可能存在錯誤。你可以發佈其餘的嗎? –