首页 > 解决方案 > Android Studio 4.1.1 资源链接失败

问题描述

我使用以下代码创建了一个 theme.xml 文件:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->

        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
</resources>

但是,每当我尝试运行它时,我都会收到以下错误:

AGPBI: {"kind":"error","text":"Android resource linking failed","sources":[{}],"original":"AAPT: C:\\Users\\hagel\\AndroidStudioProjects\\MyApplication\\app\\build\\intermediates\\incremental\\mergeDebugResources\\merged.dir\\values-night-v8\\values-night-v8.xml:28: error: resource color/purple_200 (aka com.example.myapplication:color/purple_200) not found.\nC:\\Users\\$name\\AndroidStudioProjects\\MyApplication\\app\\build\\intermediates\\incremental\\mergeDebugResources\\merged.dir\\values-night-v8\\values-night-v8.xml:29: error: resource color/purple_700 (aka com.example.myapplication:color/purple_700) not found.\nC:\\Users\\$name\\AndroidStudioProjects\\MyApplication\\app\\build\\intermediates\\incremental\\mergeDebugResources\\merged.dir\\values-night-v8\\values-night-v8.xml:30: error: resource color/black (aka com.example.myapplication:color/black) not found.\nC:\\Users\\$name\\AndroidStudioProjects\\MyApplication\\app\\build\\intermediates\\incremental\\mergeDebugResources\\merged.dir\\values-night-v8\\values-night-v8.xml:32: error: resource color/teal_200 (aka com.example.myapplication:color/teal_200) not found.\nC:\\Users\\$name\\AndroidStudioProjects\\MyApplication\\app\\build\\intermediates\\incremental\\mergeDebugResources\\merged.dir\\values-night-v8\\values-night-v8.xml:33: error: resource color/teal_200 (aka com.example.myapplication:color/teal_200) not found.\nC:\\Users\\$name\\AndroidStudioProjects\\MyApplication\\app\\build\\intermediates\\incremental\\mergeDebugResources\\merged.dir\\values-night-v8\\values-night-v8.xml:34: error: resource color/black (aka com.example.myapplication:color/black) not found.\nerror: failed linking references.\n\n    ","tool":"AAPT"}
AAPT: C:\Users\$name\AndroidStudioProjects\MyApplication\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values-night-v8\values-night-v8.xml:28: error: resource color/purple_200 (aka com.example.myapplication:color/purple_200) not found.
C:\Users\$name\AndroidStudioProjects\MyApplication\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values-night-v8\values-night-v8.xml:29: error: resource color/purple_700 (aka com.example.myapplication:color/purple_700) not found.
C:\Users\$name\AndroidStudioProjects\MyApplication\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values-night-v8\values-night-v8.xml:30: error: resource color/black (aka com.example.myapplication:color/black) not found.
C:\Users\$name\AndroidStudioProjects\MyApplication\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values-night-v8\values-night-v8.xml:32: error: resource color/teal_200 (aka com.example.myapplication:color/teal_200) not found.
C:\Users\$name\AndroidStudioProjects\MyApplication\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values-night-v8\values-night-v8.xml:33: error: resource color/teal_200 (aka com.example.myapplication:color/teal_200) not found.
C:\Users\$name\AndroidStudioProjects\MyApplication\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values-night-v8\values-night-v8.xml:34: error: resource color/black (aka com.example.myapplication:color/black) not found.
error: failed linking references.

请帮我解决这个问题,亲爱的堆栈天使。

标签: javaandroidxmlandroid-studioandroid-layout

解决方案


据我从日志中可以看出,我认为您只是在目录中定义颜色,values-night而不是在values目录中。如果提供任何特定于配置的资源,则需要在基本资源目录中定义该资源。

例如,如果您splash.xml在 drawable-v24 中定义了一些可绘制对象,那么该可绘制对象splash.xml也需要在drawable目录中定义。

让我知道这是否有帮助。


推荐阅读