问题描述
我试图让布尔从共享preferences 值,真正,但我得到一个假值每次。
命名为共享preferences文件我的preF包含以下code:
< XML版本='1.0'编码='UTF-8'独立='是'&GT?;
<地图>
<布尔NAME =preMIUMVALUE =真/>
< /图>
我在添加值到共享preferences MyActivity.class 使用以下code:
共享preferences preF = getApplicationContext()getShared preferences(我的preF,1); // 0 - 为私模
编辑编辑= pref.edit();
editor.putBoolean(preMIUM,真正的); //存储布尔 - 真/假
editor.commit(); //提交更改
Log.d(TAG,用户是+(MIS premiumpreMIUM:NOT preMIUM));
和通过检索它的多类
布尔MIS premium = FALSE;
共享preferences preF = getApplicationContext()getShared preferences(我的preF,1); // 0 - 为私模
MIS premium = pref.getBoolean(私人,FALSE); //获取布尔
Log.d(IS preMIUM,将String.valueOf(MIS premium));
如果(MIS premium){
// $ P $ USER pMIUM - 没有广告
}其他{
// NOT $ P $ USER pMIUM - 显示广告
}
我总是得到尽可能假,可能是什么原因,要做到这一点,如何解决它的 premium 的价值?我使用的Android 4.2.2。谢谢大家。
MIS premium = pref.getBoolean(preMIUM,FALSE);
不是(私人,FALSE)
您正在使用不同的密钥,他们需要匹配。
I'm trying to get a boolean value from SharedPreferences that is true but I'm getting a false value every time.The SharedPreferences file named "MyPref"contains the following code:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<boolean name="PREMIUM" value="true" />
</map>
I'm adding values to SharedPreferences in MyActivity.class using the following code:
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 1); // 0 - for private mode
Editor editor = pref.edit();
editor.putBoolean("PREMIUM", true); // Storing boolean - true/false
editor.commit(); // commit changes
Log.d(TAG, "User is " + (mIsPremium ? "PREMIUM" : "NOT PREMIUM"));
And retrieving it on multiple classes by using:
boolean mIsPremium = false;
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 1); // 0 - for private mode
mIsPremium = pref.getBoolean("PRIVATE", false); // getting boolean
Log.d("IS PREMIUM", String.valueOf(mIsPremium));
if(mIsPremium) {
// PREMIUM USER - NO ADS
} else {
// NOT PREMIUM USER - SHOW ADS
}
I'm always getting the value of Premium as false, what could be the reason for this to happen and how to solve it ? I'm using android 4.2.2. Thank you all.
mIsPremium = pref.getBoolean("PREMIUM", false);
NOT ("PRIVATE",false)
You are using different keys, they need to match.
这篇关于Android的共享preferences布尔不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!