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

前端 未结 12 2134
没有蜡笔的小新
没有蜡笔的小新 2021-02-06 23:06

I have android app and this string in resources:

Select up to %1$d people!

Thi

相关标签:
12条回答
  • 2021-02-06 23:26

    Make a function and put your code inside it and add this @SuppressLint("StringFormatInvalid") just before the function. Hope it helps.

    0 讨论(0)
  • 2021-02-06 23:29

    I had the same issue, but it has a different origin. My app has some languages and in one translation there was no "%" but a "$" sign instead. It still worked, except in this specific language and I received this hint at the respective line were I used the string.

    0 讨论(0)
  • 2021-02-06 23:32

    You need String formatter. Please change below code from

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

    to

    String temp =  String.format(getResources().getString(R.string.create_group_select_people), countMax);
    

    For more detail information refer

    0 讨论(0)
  • 2021-02-06 23:34

    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.

    0 讨论(0)
  • 2021-02-06 23:40

    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.

    0 讨论(0)
  • 2021-02-06 23:42

    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

    0 讨论(0)
提交回复
热议问题