当前位置:   article > 正文

Error:error: resource previously defined here. AAPT2 error: check logs for details

error: resource previously defined here.

如果你遇到了以下的问题:

这里写图片描述

说明你可能遇到跟我一样的问题了。

AAPT

AAPT是Android Asset Packaging Tool的缩写,即安卓文件打包工具。作用是将安卓资源文件编译成二进制文件。

问题产生

我在自定义View的时候,自定义了attr属性,添加了一个textColor的属性值,为的是接收文字颜色值。

<declare-styleable name="RollTextView">
        <attr name="textColor" format="color"/>
        <attr name="textSize" />
</declare-styleable>
  • 1
  • 2
  • 3
  • 4

结果运行的时候就出现了上述的问题,原因是因为重定义了系统的属性值。

解决办法

1、重命名textColor,名称不与系统的textColor一样即可。

<declare-styleable name="RollTextView">
        <attr name="text_color" format="color"/>
        <attr name="textSize" />
</declare-styleable>
  • 1
  • 2
  • 3
  • 4

2、不对textColor进行重命名,直接引用系统的textColor,然后在xml里面使用时,就不能使用自定义的命名空间了(例如:app:),得用使用原生的引用(android:),千里之堤,毁于蚁穴,稍加注意就好。

<declare-styleable name="RollTextView">
        <attr name="textColor" />
        <attr name="textSize" />
</declare-styleable>
  • 1
  • 2
  • 3
  • 4

特此做个小笔记。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/838244
推荐阅读
相关标签
  

闽ICP备14008679号