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

把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>

 



posted @ 2012-03-28 15:01  tt-0411  阅读(9287)  评论(0编辑  收藏  举报