您的位置:首页 > 其它

解决strings.xml格式化占位符错误 Type error: Multiple substitutions specified in non-positional format...

2012-03-28 15:01 399 查看
把Android示例项目SimpleWikitionary的代码导入Eclipse时在string.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>


上网查了一番,找到了解决办法,参考http://be-evil.org/android-multiple-substitutions-specified-in-non-positional-format.html

解决办法是:

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐