无法在sharedpreference中解析getboolean

时间:2016-03-23 19:27:20

标签: android

我想在android中使用sharedpreferrences。我把数据放到它但是当在其他活动中获取数据时,它找不到prefs.getboolean。

class A{
   SharedPreferences.Editor editor = ApplicationLoader.applicationContext.getSharedPreferences("keymode", 0).edit();
   editor.putBoolean("BotKey", true);
   editor.commit();}
class B{
  SharedPreferences prefs = ApplicationLoader.applicationContext.getSharedPreferences("keymode", 0);
  SharedPreferences.Editor editor = prefs.edit();
  if(prefs.getBoolean("BotKey",false)){}
  }

1 个答案:

答案 0 :(得分:0)

尝试使用这种方式:

将值存储在共享首选项中:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("BotKey", true);
editor.apply();

从共享首选项中检索值:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean isBotKey = preferences.getBoolean("BotKey", false);

像齐姆所说:https://stackoverflow.com/a/11027631/5090511