android中String.xml報錯–Multiple annotations found at this line

來源:互聯網
上載者:User

本文來源於:hunter的部落格(http://my.oschina.net/hunterXue/blog/15107)

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中的 % 導致編譯失敗,

這是由於新的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> 即可

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.