SharedPreferences的getString始终返回一个值的默认值

时间:2012-09-28 12:42:04

标签: android sharedpreferences

我在活动中使用此代码

 protected void onResume() {
    super.onResume();
    prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    System.out.println(connections);
    String dir = prefs.getString("download_directory", "Download");
    System.out.println(dir);        
 }

我的偏好.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”键的getString总是给我默认值“Download”

1 个答案:

答案 0 :(得分:0)

  

key“download_directory”的getString总是给我默认值   价值“下载”

只有在未设置“download_directory”时才会发生这种情况!