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