现在的位置: 首页 > 综合 > 正文

【xml String format】error: Multiple substitutions specified in non-positional format;

2017年10月29日 ⁄ 综合 ⁄ 共 666字 ⁄ 字号 评论关闭

今天大篇的粘贴arrays.xml中出现了这个问题:

Type error: 

Multiple substitutions specified in non-positional format; did you mean to add the formatted="false" attribute?

对应的代码是:

<string name="template_user_agent">"%s/%s (Linux; Android)"</string>

解决办法是:

1 使用%%或\%,如:

<string name="template_user_agent">"%%s/%%s (Linux; Android)"</string>

2 添加 formatted="false" 属性

<string name="template_user_agent" formatted="false">"%s/%s (Linux; Android)"</string>

这个错误和ADT的版本有关,旧版本不会出现这个提示。

更新修正:

用上面两种方法,会使得字符串丧失格式化的能力(比如使用 getResource().getString(String,Object...)这个方法),保持格式化字符串的做法,可参考:http://blog.csdn.net/ganggang1st/article/details/6804086,修改如下:

<string name="template_user_agent" formatted="false">"%1$s/%2$s (Linux; Android)"</string>

 

抱歉!评论已关闭.