2012-01-18 66 views
1

我遇到了一些麻煩恢復SharedPreferences到活動從原來的分開。SharedPreferences分佈在不同的活動在同一應用程序

我有被利用這一點,「NewCustomerActivity」和「OldCustomerActivity」兩班。兩者都應該具有對文件的讀/寫訪問權限 - 目前我只是通過從NewCustomerActivity中寫入來進行測試,NewCustomerActivity會在殺死活動時適當地恢復表單數據;不過,我在打開OldCustomerActivity時收到一個FC,該OldCustomerActivity嘗試用與NullPointerException相同的NewCustomerActivity來恢復數據。

NewCustomerActivity:

public class NewCustomerActivity extends Activity { 

public static final String USER_INFO = "UserInfoFile"; // This file will the store the user's information for later use. 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.newcustomer); 
    SharedPreferences userInfo = getSharedPreferences(USER_INFO, 0); // Restore our earlier saved info. 
    EditText et; 
    ...... (Other code is irrelevant) 
} 

OldActivityNew是一樣的:

public class OldCustomerActivity extends Activity { 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.oldcustomer); 

    SharedPreferences userInfo = getSharedPreferences(NewCustomerActivity.USER_INFO, 0); // Restore our earlier saved info. 
    EditText et; 

    et = (EditText) findViewById(R.id.EditTextName); 
    et.setText(userInfo.getString("name","")); 
..... (Continue to fill forms in this context) 
} 

這兩個活動的填補與以前信息的形式,如果有什麼改變了更新在提交的文件;然而只有NewCustomerActivity似乎是填充文件(或不強制關閉是具體的)

我試着在MODE_WORLD_READABLE(第二個參數= 1)設置SharedPreference以及無濟於事;即使我相信我應該可以在PRIVATE中運行它。我也嘗試引用USER_INFO作爲NewCustomerActivity.USER_INFO

我必須失去了一些東西明顯 - 但任何幫助,將感謝讚賞,因爲這是我第一次嘗試,!

編輯:

對於那些問我是怎麼寫入文件:

 SharedPreferences userInfo = getSharedPreferences(USER_INFO, 0); // Save info for later 
     SharedPreferences.Editor userInfoEditor = userInfo.edit(); 
     EditText et; 
et = (EditText) findViewById(R.id.EditTextName); 
     String nameValue = et.getText().toString(); 
     userInfoEditor.putString("name", nameValue); 
+0

如何在每個活動中寫入SharedPreference? – 2012-01-18 01:18:48

+0

這裏的一個片段: SharedPreferences USERINFO = getSharedPreferences(USER_INFO,0); //保存以後的信息 \t \t SharedPreferences。編輯器userInfoEditor = userInfo.edit(); \t \t EditText et; \t \t et =(EditText)findViewById(R.id.EditTextName); \t \t String nameValue = et.getText()。toString(); \t \t userInfoEditor.putString(「name」,nameValue); – 2012-01-18 02:44:04

+0

看起來你沒有提交寫操作。爲了讓'SharedPreferences'生效,在完成所有編輯之後,您需要執行'userInfoEditor.commit();' – curioustechizen 2012-01-18 03:02:10

回答

0

它看起來像你想使用不同的模式比你寫給得到oldCustomerActivity偏好他們。 在該類更改此行:

getSharedPreferences(NewCustomerActivity.USER_INFO, 1)

使用0 mode參數:

getSharedPreferences(NewCustomerActivity.USER_INFO, 0)

的詮釋你傳遞給函數沒有定義,如果你正在閱讀或寫作,但定義了首次寫入後首選項的行爲方式。

要編輯的喜好已加載後都需要調用:

SharedPreferences.Editor editor = userInfo .edit();

然後,您可以設置的變量是這樣的:

editor.putString("username", "Ash");

更多信息,可以發現here

+0

對不起 - 這是一個錯字。我實際上把它們都設置爲'0'(MODE_PRIVATE),並將它們更改爲'1'(MODE_WORLD_READABLE),以查看這是否會改變任何事情(假設可能是私有的意味着它必須在同一活動中,儘管文檔「我使用編輯器來編寫偏好,就像你剛纔所說的那樣(只是我沒有顯示它 - 它是在我提交表單時使用的另一個函數中)從我開始,一切似乎都被正確地寫入了可以重新加載NewCustomerActivity Activity中的數據,但是感謝輸入 – 2012-01-18 02:43:27

+0

哦!如果你在Eclipse中,然後打開Logcat,它將顯示應用程序部隊關閉時引發的錯誤。 – Jivings 2012-01-18 02:45:59

相關問題