我无法通过“Android 资源链接失败”错误

Posted

技术标签:

【中文标题】我无法通过“Android 资源链接失败”错误【英文标题】:I can't get past 'Android resource linking failed' error 【发布时间】:2021-12-03 01:33:50 【问题描述】: 错误如下
android resource linking failed
ERROR:AAPT: C:\Users\Sifundo\AndroidStud
ioProjects\LoginRegistration\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml:4612: error: resource color/mtrl_btn_text_color_selector (aka com.example.loginregistration:color/mtrl_btn_text_color_selector) not found.
C:\Users\Sifundo\AndroidStudioProjects\LoginRegistration\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml:4626: 
error: resource color/mtrl_btn_text_color_selector (aka com.example.loginregistration:color/mtrl_btn_text_color_selector) not found.
C:\Users\Sifundo\AndroidStudioProjects\LoginRegistration\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml:4612: error: resource color/mtrl_btn_text_color_selector (aka com.example.loginregistration:color/mtrl_btn_text_color_selector) not found.
C:\Users\Sifundo\AndroidStudioProjects\LoginRegistration\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml:4626: error: resource color/mtrl_btn_text_color_selector (aka com.example.loginregistration:color/mtrl_btn_text_color_selector) not found.
error: failed linking references.

我已使用代码将 mtrl_btn_text_color_selector.xml 文件添加到 res/color 文件夹:

<?xml version="1.0" encoding="utf-8"?>
<!--
    Copyright 2017 The Android Open Source Project
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
        http://www.apache.org/licenses/LICENSE-2.0
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:alpha="1.00" android:color="?attr/colorPrimary" android:state_checkable="true" android:state_checked="true" android:state_enabled="true"/>
    <item android:alpha="0.60" android:color="?attr/colorOnSurface" android:state_checkable="true" android:state_checked="false" android:state_enabled="true"/>
    <item android:alpha="1.00" android:color="?attr/colorPrimary" android:state_enabled="true"/>
    <item android:alpha="0.38" android:color="?attr/colorOnSurface"/>
</selector>    

【问题讨论】:

你能把你的 XML 代码贴在这里吗? schemas.android.com/apk/res/android"> 【参考方案1】:

您需要更正命名空间。

它应该以 http:// 开头

检查一下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:alpha="1.00" android:color="?attr/colorPrimary" android:state_checkable="true" android:state_checked="true" android:state_enabled="true" />
    <item android:alpha="0.60" android:color="?attr/colorOnSurface" android:state_checkable="true" android:state_checked="false" android:state_enabled="true" />
    <item android:alpha="1.00" android:color="?attr/colorPrimary" android:state_enabled="true" />
    <item android:alpha="0.38" android:color="?attr/colorOnSurface" />
</selector>

【讨论】:

哦,我的命名空间中确实有 http://,我粘贴代码时一定出了问题 您的代码在我的系统中运行良好。请再检查一次! 另外,你把 XML 放在哪里了?在颜色目录中? 是的,XML 文件在 color 文件夹中。只是不确定我是否有正确的代码,因为我是从 github 获取的

以上是关于我无法通过“Android 资源链接失败”错误的主要内容,如果未能解决你的问题,请参考以下文章

科尔多瓦错误构建项目:Android资源链接失败

Android Studio Android 资源链接失败错误.. AAPT:错误:找不到资源 android:attr/lStar。在 values.xml 105

错误 - Android 资源链接失败(AAPT2 27.0.3 Daemon #0)

如何解决android studio中的错误:Android资源链接失败

即使我的代码没有更改,也会出现“Android 资源链接失败”

我在构建应用程序时在 android studio 中遇到了 Android 资源链接失败错误。我不知道如何解决这个问题。我是 Android Studio 的新手 [关闭]