java - 同一应用程序中不同 Activity 之间的共享首选项

标签 java android sharedpreferences

我在将 SharedPreferences 恢复到与原始 Activity 不同的 Activity 中时遇到一些问题。

我有两个类正在利用这个,“NewCustomerActivity”和“OldCustomerActivity”。两者都应该具有对该文件的读/写访问权限 - 目前我只是通过从 NewCustomerActivity 进行写入来进行测试,这将在终止 Activity 时适本地恢复表单数据;但是,我在打开 OldCustomerActivity 时收到了 FC,它试图以与 NewCustomerActivity 相同的方式恢复数据,但出现 NullPointerException。

新客户 Activity :

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

这两项 Activity 都使用以前的信息填写表格,并在提交时更新文件(如果有任何更改);但是似乎只有 NewCustomerActivity 正在填充该文件(或者不强制关闭特定的文件)

我尝试在 MODE_WORLD_READABLE 中设置 SharedPreference (第二个参数 = 1)也无济于事;尽管我相信我应该能够以私有(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);

最佳答案

看起来您正在尝试使用与您编写的模式不同的模式来获取 oldCustomerActivity 中的首选项。 更改该类中的这一行:

getSharedPreferences(NewCustomerActivity.USER_INFO, 1)

使用 0 作为模式参数:

getSharedPreferences(NewCustomerActivity.USER_INFO, 0)

传递给函数的 int 并不定义您是在读取还是写入,而是定义了您希望首选项在首次写入后的行为方式。

要在加载首选项后编辑它们,您需要调用:

SharedPreferences.Editor editor = userInfo .edit();

然后您可以像这样设置变量:

editor.putString("用户名", "Ash");

更多信息可参见here .

关于java - 同一应用程序中不同 Activity 之间的共享首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8903936/

相关文章:

android - 在 PreferenceActivity 中的首选项更改时在 Android 主 Activity 中使用react

java - null 是什么类?

java - 为什么链表克隆方法实现需要将复制的列表存储为原始状态?

android - onMeasure 没有在我的自定义 View 组 android 中被调用

java - PendingIntent 到底什么时候被处理/删除?

android - OnSharedPreferenceChangeListener 不适用于所有设备?

java - 是否可以在 Eclipse 中突出显示一条语句,然后按引号/等 .. 到 "wrap"字符中突出显示的文本(如 SublimeText)?

java - 是否有模型/实体类的模式

Android 每 30 秒从 url 加载图像

android - 如何遍历所有 Bundle 对象