【发布时间】:2017-07-03 11:44:29
【问题描述】:
在我的 MainActivity 中,我编写了一些代码,我假设这些代码会创建一个文件并在该文件中保存一个值。
public static final String WHAT_I_WROTE = null;
public void sendMessage(View view) {
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
//creates new SharedPreference?
SharedPreferences saver = getSharedPreferences("saved_text", Context.MODE_PRIVATE);
//writes to the preferences file called saved_text?
SharedPreferences.Editor writer = saver.edit();
writer.putString(WHAT_I_WROTE, message);
writer.commit();
}
在另一个活动中,我希望能够阅读并显示消息,但是当我尝试这样做时,它无法解析符号“saver”。
String text_for_display = saver.getString(WHAT_I_WROTE);
我在这里犯了什么错误,如何纠正它以读取保存的字符串?
谢谢。
【问题讨论】:
-
为什么在共享首选项中使用 null 作为键?
-
WHAT_I_WROTE 在这里为空。应改为有效字符串。
-
请发布您从共享偏好中获取数据的其他活动的代码
-
我不完全确定 null 是什么意思。我以为你在声明字符串时必须使用它作为占位符。
标签: android sharedpreferences saving-data