简体   繁体   English

同一应用程序中各个活动之间的SharedPreferences

[英]SharedPreferences across separate activities in the same application

I'm having some trouble restored SharedPreferences into an activity separate from the original. 我在将SharedPreferences恢复到与原始活动分开的活动时遇到了麻烦。

I have two classes that are utilizing this, "NewCustomerActivity" and "OldCustomerActivity". 我有两个利用这个的类,“ NewCustomerActivity”和“ OldCustomerActivity”。 Both should have read/write access to the file - currently I'm just testing by writing from NewCustomerActivity, which will appropriately restore form data upon killing the activity; 两者都应具有对该文件的读/写访问权限-当前,我只是通过从NewCustomerActivity进行写入进行测试,这将在取消活动后适当地恢复表单数据; however I'm receiving a FC upon opening OldCustomerActivity which is attempting to restore the data in the same sense as NewCustomerActivity with a NullPointerException. 但是,我在打开OldCustomerActivity时收到了FC,它试图以与NewCustomerActivity相同的意义通过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 is the same: 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)
}

Both of the activities fill the form with previous information, and update the file upon submission if anything has changed; 这两个活动都使用以前的信息填写表格,如果有任何更改,则在提交后更新文件; however only NewCustomerActivity seems to be populating the file (Or not force closing to be specific) 但是似乎只有NewCustomerActivity似乎正在填充文件(或者不是强制关闭特定内容)

I've tried setting up the SharedPreference in MODE_WORLD_READABLE (second parameter = 1) as well to no avail; 我试图在MODE_WORLD_READABLE(第二个参数= 1)中设置SharedPreference,但无济于事。 even though I believe I should be able to run it in PRIVATE as it is. 即使我相信我也应该能够在PRIVATE中运行它。 I've also tried referencing USER_INFO as NewCustomerActivity.USER_INFO 我也尝试将USER_INFO引用为NewCustomerActivity.USER_INFO

I must be missing something obvious - but any help would be appreciated as this is my first attempt, thanks! 我肯定缺少明显的东西-但是任何帮助将不胜感激,因为这是我的第一次尝试,谢谢!

Edit: 编辑:

For those asking how I am writing to the file: 对于那些询问我如何写入文件的人:

        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);

It looks like you're trying to get the preferences in oldCustomerActivity using a different mode than that which you wrote them. 看来您正在尝试使用与oldCustomerActivity不同的方式在oldCustomerActivity获取首选项。 Change this line in that class: 在该类中更改此行:

getSharedPreferences(NewCustomerActivity.USER_INFO, 1)

to use a 0 for the mode parameter: 将0用作mode参数:

getSharedPreferences(NewCustomerActivity.USER_INFO, 0)

The int you pass to the function doesn't define if you are reading or writing, but defines how you want the preferences to behave after you first write them. 传递给该函数的int不会定义您正在读取还是正在写入,但是会定义您希望首选项写入后的行为方式。

To edit the preferences after you have loaded them you need to call: 要在加载首选项后对其进行编辑,您需要调用:

SharedPreferences.Editor editor = userInfo .edit();

You can then set the variables like this: 然后可以像这样设置变量:

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

More information can be found here . 可以在此处找到更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM