【问题标题】:SharedPreferences across separate activities in the same applicationSharedPreferences 跨同一应用程序中的不同活动
【发布时间】:2012-01-18 01:11:51
【问题描述】:

我在将 SharedPreferences 恢复为与原始活动分开的活动时遇到了一些麻烦。

我有两个类正在使用它,“NewCustomerActivity”和“OldCustomerActivity”。两者都应该具有对文件的读/写访问权限 - 目前我只是通过从 NewCustomerActivity 写入进行测试,这将在终止活动时适当地恢复表单数据;但是我在打开 OldCustomerActivity 时收到一个 FC,它试图以与具有 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);

【问题讨论】:

  • 您如何尝试在每个活动中写入 SharedPreference?
  • 这是一个 sn-p:SharedPreferences userInfo = getSharedPreferences(USER_INFO, 0); // 为以后保存信息 SharedPreferences.Editor userInfoEditor = userInfo.edit();编辑文本等; et = (EditText) findViewById(R.id.EditTextName);字符串名称值 = et.getText().toString(); userInfoEditor.putString("name", nameValue);
  • 看起来你没有提交你的写操作。要使SharedPreferences 生效,在您完成所有编辑后,您需要执行userInfoEditor.commit();
  • 感谢您的帮助 - 但正如我之前所说的;我正在正确地编写所有内容(最后我也调用了提交 - 正如我所说的那样,我只在这里发布了一个简短的 sn-p)我只是在第二个活动(OldCustomerActivity)中读取时遇到了问题跨度>

标签: java android sharedpreferences


【解决方案1】:

您似乎正在尝试使用不同于您编写它们的模式来获取oldCustomerActivity 中的首选项。 更改该类中的这一行:

getSharedPreferences(NewCustomerActivity.USER_INFO, 1)

使用 0 作为模式参数:

getSharedPreferences(NewCustomerActivity.USER_INFO, 0)

您传递给函数的 int 并没有定义您是在阅读还是在写作,而是定义了您希望首选项在您第一次编写它们后的行为方式。

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

SharedPreferences.Editor editor = userInfo .edit();

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

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

更多信息可以在here找到。

【讨论】:

  • 对不起 - 这是一个错字。实际上,我最初将它们都设置为“0”(MODE_PRIVATE)并将它们更改为“1”(MODE_WORLD_READABLE)以查看是否会改变任何内容(假设可能私有意味着它必须在同一个活动中,尽管文档状态应用程序。'我也使用编辑器来编写您所说的首选项(只是我没有显示它 - 它在我提交表单时使用的另一个函数中)自从我可以在 NewCustomerActivity Activity 中重新加载数据,但感谢输入
  • 哦,好吧!如果您在 Eclipse 中,则打开 Logcat,它将显示应用程序强制关闭时引发的错误。
猜你喜欢
  • 1970-01-01
  • 2012-05-19
  • 2020-09-25
  • 1970-01-01
  • 2012-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-22
相关资源
最近更新 更多