Format string XXX is not a valid format string so it should not be passed to String.format

25,950

Solution 1

I just copied the code and it works well. so you may need to check some other place,Here are my suggestions.

  1. clean project
  2. check multi-language files
  3. or just use String.format just like others said

Solution 2

Set parameter formatted to true in resources:

<string name="some_text" formatted="true">
    Use for String.format method. Parameter one: %s1
</string>

and use this way:

String.format(context.getString(R.string.some_text,"value 1"))

or this way:

context.getString(R.string.some_text,"value 1"))

Note:formatted flag should be set to true only for strings with placeholders

Solution 3

This error will be shown if you have the same string in multiple string files (translations), but one of them doesn't have proper format like missing "%s" or "%1$s", which will be used to place paramters passed (ex: "countMax") in below line.

getResources().getString(R.string.create_group_select_people, countMax)

So, please check that before you are going to try any other answers mentioned above.

Solution 4

Try File -> Invalidate Caches / Restart..., it fixed the problem for me.

Solution 5

For the sake of others who might find this thread, one possible cause of this warning is that you have multiple languages defined in string resource files and you did not specify format arguments in one or more of them.

For example, if you have a strings.xml in your values folder, and another strings.xml in your values-es folder, but you only added format arguments to the strings.xml in your values folder, then the warning will be triggered because of the lack of format arguments in the string resource of strings.xml in your values-es folder.

Share:
25,950
Wackaloon
Author by

Wackaloon

Updated on July 09, 2022

Comments

  • Wackaloon
    Wackaloon almost 2 years

    I have android app and this string in resources:

    <string name="create_group_select_people">Select up to %1$d people!</string>
    

    This is called from fragment:

    Integer countMax = 5; //also tried just "int" - nothing changed
    getResources().getString(R.string.create_group_select_people, countMax);
    

    but I got error:

    Format string 'create_group_select_people' is not a valid format string so it should not be passed to String.format
    

    I can't understand what is wrong? When I launch app - it shows me literally "Select up to %1$d people!"

  • COYG
    COYG over 5 years
    Clean build worked for me. Why does this work do you know? What went wrong in the first instance.
  • Nicolás Carrasco-Stevenson
    Nicolás Carrasco-Stevenson over 5 years
    Number 2 did it for me. At first I thought there was a problem with lint because I was seeing the same warning three times, but it turns out it was one for each of the languages were the string wasn't formatted correctly
  • Chitrang
    Chitrang almost 5 years
    I'm not sure about others but for me, there is an issue with the percentage(%) symbol may be it is related to Unicode. There should be a small symbol (compart.com/en/unicode/U+FE6A) not the big symbol(compart.com/en/unicode/U+0025). Hope this helps :)
  • User
    User about 4 years
    This did it for me. For some reason, I needed to restart Android Studio to remove the error message.
  • Admin
    Admin almost 4 years
    For me I needed to check option 2 : check multi-language files
  • alex bird
    alex bird over 3 years
    If you have a percent sign you need to escape it as %%. You don't need to use an unusual unicode variant of percent as @Chitrang suggests, and if you do you may find it is missing from some fonts.
  • Yohanim
    Yohanim about 3 years
    sometimes others forget what the meaning of the second parameter
  • nesibeyyubov
    nesibeyyubov over 2 years
    it worked, thanks :)