我有一个字符串数组,其中使用了%符号.使用%的正确格式是%.当数组中有一个字符串有多个%时,就会出现这个错误.

 Multiple annotations found at this
 line:
 - error: Multiple substitutions specified in non-positional format;
   did you mean to add the formatted="false" attribute?
 - error: Found tag </item> where </string-array> is expected

推荐答案

Androidassets资源 打包工具(aapt)已经变成very strict in its latest release,现在用于all个Android版本.您得到的AAPT-ERROR是生成的,因为它不再允许non-positional format specifiers.

下面是一些如何在资源字符串中包含%-符号的方法.

如果字符串中不需要任何格式说明符或替换,只需使用formatted属性并将其设置为false:

<string formatted="false">%a + %a == 2%a</string>

在这种情况下,字符串不会用作Formatter的格式字符串,因此不必转义符号.结果字符串为"%a+%a==2%a".

如果省略formatted="false"属性,该字符串将用作格式字符串,并且必须转义符号.这是正确的使用双-%:

<string>%%a + %%a == 2%%a</string>

现在aapt不会给您任何错误,但根据您如何使用它,如果在没有任何格式arguments的情况下调用Formatter,则结果字符串可以是"%%a+%%a==2%%a":

Resources res = context.getResources();

String s1 = res.getString(R.string.str);
// s1 == "%%a + %%a == 2%%a"

String s2 = res.getString(R.string.str, null);
// s2 == "%a + %a == 2%a"

如果没有任何XML和代码,很难说出您的问题到底是什么,但希望这能帮助您更好地理解机制.

Android相关问答推荐

无法使用MenuItemColors.Copy()

为什么Ionic-checkbox不总是刷新Android上的视图?

如何阻止Gradle在编译时生成app-metadata.properties

在Jetpack Compose中的隐藏状态栏后面绘制

在Jetpack Compose中将导航绘图显示在顶部栏下方、底部栏上方

android回收器查看点击事件无响应

当提供非状态对象时,compose 如何进行重组

activity在 Runnable 中如何工作?我的 Android 表格未显示

Android:使用依赖项 ViewModelProviderFactory 初始化 ViewModel 的正确方法

为什么我要使用 $version 而不是2.7.0?

Jetpack Compose 动画的行为是什么?

在 Jetpack Compose 中重用具有重复代码的列

在 Jetpack Compose 中使用 .observeAsState() 时,如何在更改 MutableLiveData 的值后开始执行一段代码?

从活动共享视图模型以使用 hilt 组合函数

ImageBitmap 使用 Glide/Coil 到画布

复用 RecyclerView 适配器,避免不必要的 API 调用

Android Studio (Kotlin):无法启动活动

如何从构建的流对象中发出值

如何在android studio 2021.1中使用谷歌库以外的库

lambda 函数中的类型不匹配 - Kotlin