`
zhoujianghai
  • 浏览: 434336 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Android编译出现Multiple substitutions specified in non-positional format

阅读更多

前段时间更新了ADT和Android SDK 2.3,更新后发现自己2.2下编译的很好的项目出现了问题,错误详细内容为:

Multiple annotations found at this line:
    - error: Multiple substitutions specified in non-positional format; did you mean to add the formatted="false" 
     attribute?
    - error: Unexpected end tag string

 

 

反复检查后发现是string.xml中的 % 导致编译失败,

去Google的Android开发者讨论组查看关于这个问题的讨论 后看到了Xavier Ducrohet大神的回复,说这是由于新的SDK采用了新版本的aapt(Android项目编译器),这个版本的aapt编译起来会比老版本更加的严格,然后在Android最新的开发文档的描述String的部分,已经说明了 如何去设置 %s 等符号,下面是文档片段:

If you need to format your strings using String.format(String, Object...) , then you can do so by putting your format arguments in the string resource. For example, with the following resource:

如果你需要使用 String.format(String, Object...) 来格式化你的字符串,你可以把格式化参数放在你的字符串中,参见下面的例子:

<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>

In this example, the format string has two arguments: %1$s is a string and %2$d is a decimal number. You can format the string with arguements from your application like this:

在这个例子中,这个格式化的字符串有2个参数, %1$s是个字符串 %2$d 是个浮点数,你可以在你的程序中按照下面的方法来根据参数来格式化字符串:

Resources res = getResources();

String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);

那么根据例子上说的我需要把%s换成%1$s才行了,修改后编译通过,程序成功启动。

问题补充:

有读者问如何在<string></string>中使用%号

有两个办法可供选择

1.用%%来表示1个%,和转意符号 \ 的用法相同
 
2.如果你的字符串不需要格式化,可以在你的<string 标签上增加一个属性:formatted="false"例如 <string name="test" formatted="false">% test %</string> 即可

最后稍微延伸一下这个格式化变化的原因: 大神Bob Kerns 觉得当前Java的格式化字符串方式可读性不好(我也有同感),然后就写了篇博文来提出他认为的最好的方式,

http://bobkerns.typepad.com/bob_kerns_thinking/2010/12/dear-java-better-text-formatting-please.html

显然,Android开发团队认可了他的建议

文章转载自:http://be-evil.org/post-240.html

 

分享到:
评论

相关推荐

    高速PCB设计指南(二)

    本文介绍,许多人把芯片规模的BGA封装看作是由便携式电子产品所需的空间限制的一个可行的解决方案,它同时满足这些产品更高功能与性能的要求。为便携式产品的高密度电路设计应该为装配工艺着想。

    XKCD换人 - COMPLETE「XKCD Substitutions - COMPLETE」-crx插件

    根据XKCD替代漫画替换所有网页中的单词和短语。 现在,v1.0发行版更适合人类消费142%! ... 尽管确实存在实现这些替换中的一个或两个的其他扩展,但是我建立此扩展供个人使用,因为我觉得有些事情可以更智能地完成。...

    Na-site Substitutions in Cobalt Oxyhydrate Superconductors

    水合钴氧化物超导体的钠位替代研究,曹光旱,蒋帅,采用离子交换后处理,对水合钴氧化物超导体进行了钠位替代研究。 结果表明,用Li+、K+和NH4+进行离子交换导致双层水结构被破坏;而用M

    XKCD Substitutions - COMPLETE-crx插件

    语言:English 根据xkcd替换漫画替换所有网页中的单词和短语。 现在的142%适合v1.0的释放人员消费!... 虽然存在实现其中一个或两个替换的其他扩展,但我为个人使用建立了这个扩展,因为我觉得有些事情可以更聪明地完成...

    Python Regular Expression

    This module exports the following functions: match Match a regular expression pattern to the beginning of a string. search Search a string for the... escape Backslash all non-alphanumerics in a string.

    nginx源码编译所需文件合集(echo模块、headers模块、substitutions_filter模块).zip

    nginx1.12.2源码、pcre-8.4.3源码、zlib-1.2.11源码包、openssl-1.0.2r源码包、echo-nginx-module-master模块源码、headers-more-nginx-module-master源码包、ngx_http_substitutions_filter_module源码包;...

    XKCD substitutions-crx插件

    语言:English 让web成为一个陌生人更好的地方。... http://chrome.google.com/webstore/detail/skeyboardleopard/oheflacdocadefgdpiimpapbkomhgbbe. 来源:https://github.com/posnet/xkcd-substitutions

    XKCD Substitutions Part II-crx插件

    语言:English 根据最新的XKCD漫画的建议,将单词和短语替换为“更有趣”的新闻阅读! https://xkcd.com/1625/… 根据最新的XKCD漫画的建议,将单词和短语替换为“更有趣”的新闻阅读! ... 玩得开心! ...

    Substitutions-crx插件

    用更有趣的单词,短语和图片替换一些单词,短语和图片。 一个简单的扩展。 启用后,它将对所有网页进行各种替换,以使它们更有趣。 仅启用或禁用设置。 最初,这是基于xkcd漫画#1288,#1625和#​​1679的,但现在...

    nginx-cdn:CentOS一键安装Nginx

    XCDNCentOS一键安装Nginx环境要求CentOS 6/7当前版本nginx/1.18已编译模块--with-stream--with-...

    XKCD换人3「XKCD Substitutions 3」-crx插件

    使用XKCD漫画中的替换列表替换您访问的网站上的单词和短语。 默认替换基于XKCD漫画1288、1625和1679,但是可以在选项页面中自定义列表。 可以以无提示,带下划线或突出显示的方式进行替换,以引起对更改文本的注意。...

    Parallel Robots With Unconventional Joints: Kinematics and Motion Planning

    This book shows how, through certain geometric transformations, some of the standard joints used in parallel robots can be replaced with lockable or non-holonomic joints. These substitutions allow for...

    SJW Substitutions-crx插件

    语言:English 使网络更美好 这将所有提到的“ SJW”替换为“ Skeleton”。 此处的代码:https://github.com/dz/xkcd-substitutions基于:https://github.com/Posnet/xkcd-substitutions

    SJW换人「SJW Substitutions」-crx插件

    使网络更好的地方 这将所有提到的“ SJW”替换为“ Skeleton”。 此处的代码:https://github.com/dz/xkcd-substitutions基于:https://github.com/Posnet/xkcd-substitutions 支持语言:English

    EdTech换人「EdTech Substitutions」-crx插件

    使阅读EdTech的博客和新闻更有趣。 基于流行的XKCD Substitutions扩展,EdTech Substitutions取代了EdTech,教育和启动流行语,带来了可笑的后果。 支持语言:English (United States)

    XKCD替换「XKCD substitutions」-crx插件

    让网络变得更加陌生。 这个消息使你失望吗?...http://chrome.google.com/webstore/detail/skeyboardleopard/oheflacdocadefgdpiimpapbkomhgbbe来源:https://github.com/Posnet/xkcd-substitutions 支持语言:English

    xkcd Substitutions Plus-crx插件

    语言:English (United States) 使新闻更有趣的替换。 - 添加您自己的替换! (选项页面) - 打开/关闭分机 版本3.0. - 添加了替代Word部分的选项(不仅完整的单词)。 此选项还支持非英语单词替换。...

    XKCD加拿大换人「XKCD Canadian Substitutions」-crx插件

    http://chrome.google.com/webstore/detail/skeyboardleopard/oheflacdocadefgdpiimpapbkomhgbbe http://chrome.google.com/webstore/detail/xkcd-substitutions/jkgogmboalmaijfgfhfepckdgjeopfhk来源:...

    XKCD Canadian Substitutions-crx插件

    http://chrome.google.com/webstore/detail/skeyboardleopard/oheflacdocadefgdpiimpapbkomhgbbe http://chrome.google.com/webstore/detail/xkcd-substitutions/jkgogmboalmaijfgfhfepckdgjeopfhk来源:...

    My XKCD Substitutions-crx插件

    语言:English 此Chrome扩展程序使用XKCD中的替代...这是alec.posney对出色应用程序的非常苍白的模仿,请在此处查看:https://chrome.google.com/webstore/detail/xkcd-substitutions/jkgogmboalmaijfgfhfepckdgjeopfhk

Global site tag (gtag.js) - Google Analytics