java 格式字符串 XXX 不是有效的格式字符串,因此不应将其传递给 String.format
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40765890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Format string XXX is not a valid format string so it should not be passed to String.format
提问by Wackaloon
I have android app and this string in resources:
我在资源中有 android 应用程序和这个字符串:
<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!"
我不明白有什么问题?当我启动应用程序时 - 它从字面上向我显示“最多选择 %1$d 个人!”
采纳答案by ohdroid
I just copied the code and it works well. so you may need to check some other place,Here are my suggestions.
我只是复制了代码,它运行良好。所以你可能需要检查其他地方,这是我的建议。
- clean project
- check multi-language files
- or just use String.format just like others said
- 清洁工程
- 检查多语言文件
- 或者像其他人说的那样使用 String.format
回答by MiguelSlv
Set parameter formatted to true in resources:
在资源中设置参数格式为 true:
<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
注意:formatted 标志应仅对带有占位符的字符串设置为 true
回答by Bernd Kampl
Try File -> Invalidate Caches / Restart...
, it fixed the problem for me.
尝试File -> Invalidate Caches / Restart...
,它为我解决了问题。
回答by smitty1
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.
例如,如果您的 values 文件夹中有一个 strings.xml,您的 values-es 文件夹中有另一个 strings.xml,但您只向 values 文件夹中的 strings.xml 添加了格式参数,则将触发警告,因为您的 values-es 文件夹中 strings.xml 的字符串资源中缺少格式参数。
回答by Venu G.
Try doing a 'clean project' followed by a close and reopen of Android Studio.
尝试做一个“干净的项目”,然后关闭并重新打开 Android Studio。
That fixed it for me, it looks like some minor Android Studio /Lint bug.
为我修复了它,它看起来像一些小的 Android Studio / Lint 错误。
回答by Shadow Droid
You need String formatter. Please change below code from
你需要String formatter。请更改以下代码
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
有关更多详细信息,请参阅
回答by sHaRkBoY
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.
如果您在多个字符串文件(翻译)中有相同的字符串,但其中一个没有正确的格式,例如缺少将用于放置参数的“%s”或“%1$s”,则会显示此错误在下面的行中通过(例如:“countMax”)。
getResources().getString(R.string.create_group_select_people, countMax)
getResources().getString(R.string.create_group_select_people, countMax)
So, please check that before you are going to try any other answers mentioned above.
因此,在尝试上述任何其他答案之前,请先检查一下。
回答by Julian
You're probably missing the format method of the String class, If you're setting it in a TextView, the proper way is:
您可能缺少 String 类的 format 方法,如果您在 TextView 中设置它,正确的方法是:
textView.setText(String.format(getResources().getString(R.string.create_group_select_people), countMax));
回答by Braian Coronel
For the formatting of the string to be correct:
为了使字符串的格式正确:
String.format(Locale.ENGLISH, itemView.context.getString(R.string.XXX), "%17")
Also the special characters do not have to interfere with the injection of parameters. For that, take into account the exhaust outletsfor special characters
此外,特殊字符不必干扰参数的注入。为此,请考虑特殊字符的排气口
From: <string name="XXX">% %1$s</string>
来自:<string name="XXX">% %1$s</string>
To: <string name="XXX">%1$s</string>
至:<string name="XXX">%1$s</string>
Finally XXX is "%17"
最后 XXX 是“%17”
Good Luck.
祝你好运。
回答by Shivam Maindola
Make a function and put your code inside it and add this @SuppressLint("StringFormatInvalid")
just before the function. Hope it helps.
创建一个函数并将您的代码放入其中并@SuppressLint("StringFormatInvalid")
在该函数之前添加它。希望能帮助到你。