How to get Boolean value from Object
Asked Answered
P

4

7

I tried different ways to fix this, but I am not able to fix it. I am trying to get the Boolean value of an Object passed inside this method of a checkBox:

public boolean onPreferenceChange(Preference preference, Object newValue) 
{
    final String key = preference.getKey();
    referenceKey=key;
    Boolean changedValue=!(((Boolean)newValue).booleanValue()); //ClassCastException occurs here
}

I get:

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean

Prospector answered 17/5, 2012 at 20:3 Comment(1)
First you are assuming newValue is of type Boolean. You can use instanceof to check the type of newValue. If it is a boolean, with autounboxing, you have no problem. If it's a string, you can compare (ignoring case) for values "true", "false", then create the appropriate boolean representation.Snappy
M
9

Instead of casting it, you can do something like

 Boolean.parseBoolean(string);
Masterwork answered 17/5, 2012 at 20:4 Comment(3)
you could also check for instanceof Boolean and instanceof String if you needed to handle each case... but doesnt look like that hereWeight
This only takes strings not objectsSchrader
And he is pretty much only working with strings it would seem, therefore my solution.Masterwork
T
4

Here's some of the source code for the Boolean class in java.

// Boolean Constructor for String types.
public Boolean(String s) {
    this(toBoolean(s));
}
// parser.
public static boolean parseBoolean(String s) {
    return toBoolean(s);
}
// ...
// Here's the source for toBoolean.
// ...
private static boolean toBoolean(String name) { 
    return ((name != null) && name.equalsIgnoreCase("true"));
}

So as you can see, you need to pass a string with the value of "true" in order for the boolean value to be true. Otherwise it's false.

assert new Boolean( "ok" ) == false; 
assert new Boolean( "True" ) == true;
assert new Boolean( "false" ) == false;

assert Boolean.parseBoolean( "ok" ) == false; 
assert Boolean.parseBoolean( "True" ) == true;
assert Boolean.parseBoolean( "false" ) == false;
Truth answered 17/5, 2012 at 20:11 Comment(0)
R
3

From the code you posted, and the result you are seeing, it doesn't look like newValue is a boolean. So you try to cast to a Boolean, but it's not one, so the error occurs.

It's not clear what you're trying to do. Ideally you'd make newValue a boolean. If you can't do that, this should work:

boolean newValue;
if (newValue instanceof Boolean) { 
    changedValue = newValue; // autoboxing handles this for you
} else if (newValue instanceof String) {
    changedValue = Boolean.parseBoolean(newValue);
} else { 
    // handle other object types here, in a similar fashion to above
}

Note that this solution isn't really ideal, and is somewhat fragile. In some instances that is OK, but it is probably better to re-evaluate the inputs to your method to make them a little cleaner. If you can't, then the code above will work. It's really something only you can decide in the context of your solution.

Royal answered 17/5, 2012 at 20:15 Comment(0)
D
1

If you know that your Preference is a CheckBoxPreference, then you can call isChecked(). It returns a boolean, not a Boolean, but that's probably close enough.

Here is some code from the APIDemos Device Administration sample (DeviceAdminSample.java).

private CheckBoxPreference mDisableCameraCheckbox;

public void onResume() {
    ...
    mDPM.setCameraDisabled(mDeviceAdminSample, mDisableCameraCheckbox.isChecked());
    ...
}

public boolean onPreferenceChange(Preference preference, Object newValue) {
...
    boolean value = (Boolean) newValue;
...
    else if (preference == mDisableCameraCheckbox) {
        mDPM.setCameraDisabled(mDeviceAdminSample, value);
        reloadSummaries();
    }
    return true;
}
Dictation answered 17/5, 2012 at 20:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.