我是Android新手,一直在Android 2.3.3(API Level 10)仿真器上嘗試使用Yamba(微博客戶端)。正如我們所知,Twitter這樣的微博有一個時間表,但不會在沒有用戶的用戶名和密碼的情況下獲取更新。因此,在TimelineActivity(主入口點)的onCreate()方法,我有以下代碼:android.content.SharedPreferences'getString(key,defValue)方法的行爲與預期不符
if (yamba.getPrefs().getString("username", null) == null) {
startActivity(new Intent(this, PrefsActivity.class));
Toast.makeText(this, R.string.msgSetupPrefs, Toast.LENGTH_LONG).show();
}
的getprefs()
方法將得到一個SharedPreferences對象,這裏是對的getString
public abstract String getString (String key, String defValue)
Since: API Level 1
Retrieve a String value from the preferences.
Parameters
key The name of the preference to retrieve.
defValue Value to return if this preference does not exist.
Returns Returns the preference value if it exists, or defValue.
Throws ClassCastException if there is a preference with this name that is not a String.
的文檔
但是,情況並非如此。如果用戶名爲空,則getString方法將返回一個空字符串(「」),而不是如上所定義的「defValue」。在將第二個(空)更改爲(「」)後,它將與用戶啓動應用程序時首先顯示的首選項視圖一起使用。那麼爲什麼該方法不像文檔所說的那樣?
沒有什麼方法返回時,你變成 (」」)? – androidnoob
對不起,讓我澄清一下,我將第二個空值更改爲「」,現在將執行if塊中的代碼 – manuzhang