【发布时间】:2014-01-25 01:15:23
【问题描述】:
我试图在我的第二个活动中使用 edittexts 来更改我的第一个/主要活动的字符串。因此,要做到这一点,必须使用 SharedPreferences。
在我的第二个活动的顶部,我宣布了他们和一个编辑。它会导致 nullpointexception 错误并使代码崩溃。我不确定在哪里初始化它,因为我希望在主要/第一个活动中查看 sharedPreferences。
SharedPreferences settings = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = settings.edit();
另外,将这段代码放入 sharedprefs 字典中是否合适?
if(!introstring.isEmpty()) //if the fields are NOT empty, they should get saved.
{
editor.putString("intro", introstring);
}
【问题讨论】:
-
你能发布第一个和第二个活动代码吗?在活动的 oncreate 方法中声明的共享首选项