解決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>

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM