简体   繁体   中英

How to get Boolean value from Object

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

而不是铸造它,你可以做类似的事情

 Boolean.parseBoolean(string);

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;

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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM