2012-09-28 37 views
0

我使用一個活動驗證碼的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給我的默認值「下載」

+0

您可以在設置值時發佈代碼嗎? – zabawaba99

+0

您的preferences.xml的其餘部分可能存在錯誤。你可以發佈其餘的嗎? –

回答

0

的getString關鍵「download_directory」總是給我默認 值「下載」

如果「download_directory」沒有被設置在所有這隻能發生!

+0

似乎如果我把一個自定義的價值它的作品。我認爲XML代碼中的defaultValue會通過getString/Int方法傳遞,如果我不更改它們,並且方法中的第二個參數只會在方法無法檢索值時使用,而不是方法返回他的第二個參數,如果我不要從設置中更改該值,我希望我解釋自己 –