0
我剛剛設法刪除了我最後一個問題,因爲這裏又是一次。來自其他課程的Android調用偏好
我有下面的課,我一起去測試偏好課。我面臨的問題是,當我從偏好班傳遞或呼叫時,沒有任何反應。代碼編譯良好,但我無法獲取或設置我的首選項。它似乎陷入了SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
,因爲我已經使用了一些打印語句來查看它停在哪裏。我曾嘗試使用Android網站上的示例,但這並沒有幫助。任何人有任何想法?
public class Preferences extends Activity{
public void getSetPrefs(){
Preferences p = new Preferences();
p.setLocationPref("london", "1");
String location = p.getLocation();
String location_id = p.getLocation();
}
}
// Preferences類
public class Preferences extends Activity{
/** Sets location preferences */
public void setLocationPref(String location, int location_id){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("location", location);
editor.putInt("location_id", location_id);
editor.commit();
}
/** Get location from preferences */
public String getLocation(){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
String location = sharedPreferences.getString("location", "");
return location;
}
/** Get location ID from preferences */
public int getLocationID(){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
int locationID = sharedPreferences.getInt("location_id", 0);
return locationID;
}
}
嘗試張貼。你可能會得到一個國防部回滾 – gobernador
正如我在你以前的問題中問,是否有一個原因,你沒有使用getDefaultSharedPreferences(this)? – Tushar
對不起,刪除它,並不意味着。只要讀一讀就可以了。如果你得到一個簡單的例子,你知道我會感激:) – James