سؤال

I am writing an Android app and have an issue with defining a boolean resource.

I have a file, bools.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="preferences_autoplay">true</bool>
</resources>

Yet when I try to use R.bool.preferences_autoplay, it is recognized as an int and not a boolean:

enter image description here

And I get the error:

The method putBoolean(String, boolean) in the type SharedPreferences.Editor is not applicable for the arguments (String, int)

I can certainly make due with using an int if I need to, but I don't understand why it isn't recognized as a boolean.

Any thoughts on how to use a boolean resource as a boolean?

هل كانت مفيدة؟

المحلول

The generated R fields refer to the ID of the specified boolean (or other types). You can resolve the actual value using Resources.getBoolean. E.g.:

boolean ap = context.getResources().getBoolean(R.bool.preferences_autoplay);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top