I’m having a really frustrating problem with SharedPreference.getBoolean in android. See the following snippet:
Map<String, ?> all = preferences.getAll();
Object x = all.get("EnableMedia");
boolean v = preferences.getBoolean("EnableMedia", (Boolean) null);
I can see in the debugger that ‘x’ is a Boolean and it is true.
Yet, if I execute the next line, preferences.getBoolean, it throws an exception. What gives?!
Look at this call:
Now look at the signature of
getBoolean:Note that it’s a
booleanvalue, not aBooleanvalue. So what’s actually happening is your code is something like this:That will throw a
NullPointerException, as you’re calling a method on a null reference.You need to pass in a valid
booleanvalue, e.g.