Reputation: 1066
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?!
Upvotes: 0
Views: 1743
Reputation: 1502296
Look at this call:
preferences.getBoolean("EnableMedia", (Boolean) null);
Now look at the signature of getBoolean
:
public abstract boolean getBoolean (String key, boolean defValue)
Note that it's a boolean
value, not a Boolean
value. So what's actually happening is your code is something like this:
Boolean tmp = null;
preferences.getBoolean("EnableMedia", tmp.booleanValue());
That will throw a NullPointerException
, as you're calling a method on a null reference.
You need to pass in a valid boolean
value, e.g.
preferences.getBoolean("EnableMedia", true);
Upvotes: 4