Search code examples
androidlistformatpreference

Symbol '%' causes crash when used in android:summary tag


I have a declaration of a preference list in a preference.xml file:

<ListPreference
        android:key="pref_reflected_calibration"
        android:title="Calibration constant"
        android:summary="Select 18% for DSLR (default), 12.5% for Sekonics"
        android:entries="@array/reflected_calibration_entries"
        android:entryValues="@array/reflected_calibration_values"
        android:defaultValue="18"/>

It causes the crash with error: "java.util.IllegalFormatConversionException: %f can't format java.lang.String arguments"

When I remove '%' symbol from android:summary tag, it works ok. Replacing '%' symbol with a unicode equivalent doesn't help. Couldn't find explanation in documentation, so ask this question here


Solution

  • In Java a % symbol usually precedes a format specifier, this is what probably causes the error. Escaping % symbols with another % should probably solve the issue:

    android:summary="Select 18%% for DSLR (default), 12.5%% for Sekonics"