【问题标题】:Failed linking file resources链接文件资源失败
【发布时间】:2018-01-31 19:02:26
【问题描述】:

这是给出错误的 java 文件

package com.example.daksh.timetable;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button mon_but,tue_but,wed_but,thur_but,fri_but;
        mon_but = (Button)findViewById(R.id.mon);
        tue_but = (Button)findViewById(R.id.tue);
        wed_but = (Button)findViewById(R.id.wed);
        thur_but = (Button)findViewById(R.id.thur);
        fri_but = (Button)findViewById(R.id.fri);
        final ImageView main_Image = (ImageView) findViewById(R.id.day);
        final int[] dayarray = {R.drawable.monday,R.drawable.tuesday,R.drawable.wednesday,R.drawable.thursday, R.drawable.friday};

        mon_but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                main_Image.setImageResource(dayarray[0]);
            }

        });
        tue_but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                main_Image.setImageResource(dayarray[1]);
            }
        });
        wed_but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                main_Image.setImageResource(dayarray[2]);
            }
        });
        thur_but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                main_Image.setImageResource(dayarray[3]);
            }
        });
        fri_but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                main_Image.setImageResource(dayarray[4]);
            }
       });
   }
}

我得到的错误如下

信息:Gradle 任务 [clean, :app:assembleDebug] C:\Projects\TimeTable\app\src\main\res\layout\activity_main.xml 错误:错误:资源 android:attr/colorSwitchThumbNormal 是私有的。 错误:资源 android:attr/colorSwitchThumbNormal 是私有的。 C:\Projects\TimeTable\app\src\main\res\layout-land\activity_main.xml 错误:错误:资源 android:attr/colorSwitchThumbNormal 是私有的。 错误:资源 android:attr/colorSwitchThumbNormal 是私有的。 错误:链接文件资源失败。 错误:java.util.concurrent.ExecutionException:java.util.concurrent.ExecutionException:com.android.tools.aapt2.Aapt2Exception:AAPT2 错误:查看日志了解详细信息 错误:java.util.concurrent.ExecutionException:com.android.tools.aapt2.Aapt2Exception:AAPT2 错误:检查日志以获取详细信息 错误:com.android.tools.aapt2.Aapt2Exception:AAPT2 错误:查看日志了解详细信息 错误:任务“:app:processDebugResources”执行失败。 执行 aapt 失败 信息:BUILD FAILED in 9s 信息:9 个错误 信息:0 个警告 信息:在控制台中查看完整的输出

这是 Activity_main.xml 文件

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/day"
        android:layout_width="0dp"
        android:layout_height="0dp"

        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:contentDescription="TODO"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/guideline_dayselection"
        app:layout_constraintHorizontal_bias="0.519"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline2"
        app:layout_constraintVertical_bias="0.472"
        app:srcCompat="@drawable/monday" />

    <TextView
        android:id="@+id/textView"
        style="@style/Widget.AppCompat.TextView.SpinnerItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:fontFamily="serif"
        android:text="@string/message"
        android:textColor="?android:attr/colorSwitchThumbNormal"
        android:textSize="18sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/mon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="50dp"
        android:text="@string/monday"
        app:layout_constraintBottom_toTopOf="@+id/tue"
        app:layout_constraintStart_toStartOf="@+id/guideline_dayselection"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/tue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginStart="16dp"
        android:text="@string/tuesday"
        app:layout_constraintBottom_toTopOf="@+id/wed"
        app:layout_constraintStart_toStartOf="@+id/guideline_dayselection"
        app:layout_constraintTop_toBottomOf="@id/mon" />

    <Button
        android:id="@+id/wed"
        android:layout_width="88dp"
        android:layout_height="48dp"
        android:layout_marginBottom="16dp"
        android:layout_marginStart="16dp"
        android:text="@string/wednesday"
        app:layout_constraintBottom_toTopOf="@+id/thur"
        app:layout_constraintStart_toStartOf="@+id/guideline_dayselection"
        app:layout_constraintTop_toBottomOf="@id/tue" />

    <Button
        android:id="@+id/thur"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginStart="16dp"
        android:text="@string/thursday"
        app:layout_constraintBottom_toTopOf="@+id/fri"
        app:layout_constraintStart_toStartOf="@+id/guideline_dayselection"
        app:layout_constraintTop_toBottomOf="@id/wed" />

    <Button
        android:id="@+id/fri"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="16dp"
        android:text="@string/friday"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline_dayselection"
        app:layout_constraintTop_toBottomOf="@id/thur" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline_dayselection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_end="196dp" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.15339233" />


</android.support.constraint.ConstraintLayout>

【问题讨论】:

    标签: android-resources android-studio-3.0


    【解决方案1】:

    您的java 文件可能出现此错误,因为有一个或多个XML 文件出现错误。

    检查所有XML 文件并解决错误,然后从构建菜单中查看cleanrebuild 项目

    从您最近编辑的XML 文件开始

    【讨论】:

    • 是的,有时通过清理或重建项目解决了问题。但它不是每次都工作应该检查它最近添加的文件。 快乐学习..
    • 我在 XML 文件中遇到了 IDE 未突出显示的问题。不得不请我的同事为我编译,并在他的 IDE 中显示。越野车 Android Studio。
    • 最好避免在没有构建或运行的情况下修改大量 xml 文件。当你运行你的项目并在修改大量 xml 文件后出现此错误时,你将很难找出哪个出错了。
    • 在大多数情况下,由于drawable/xml文件而发生此错误
    • 只需使用 CTRL + Z 执行您在 XML 页面上所做的操作即可跟踪/检测导致此错误的原因。
    【解决方案2】:

    如果有人读到这里有同样的问题,我最近也遇到过这种情况,是因为xml header被错误写入了两次:

    <?xml version="1.0" encoding="utf-8"?>
    <?xml version="1.0" encoding="utf-8"?> <!-- Remove this one -->
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
         <solid android:color="@color/mug_blue"/>
         <corners android:radius="@dimen/featured_radius" />
    </shape>
    

    我遇到的错误与此文件完全无关,因此很难找到。只要确保所有新的 xml 文件都没有这样的错误(因为它不会显示为错误)。 编辑现在似乎显示为错误,请务必检查您的错误日志。

    【讨论】:

    • 谢谢,我最近创建的文件有问题。
    • 修复在这里..!
    • 由于没有 XML 标头,我遇到了同样的错误。您的评论将我引向了问题所在。
    【解决方案3】:

    我在评论中已经提到的一种可能的解决方案:

    我在 XML 文件中遇到了 IDE 未突出显示的问题。不得不问我 同事为我编译,它显示在他的 IDE 中。越野车安卓 工作室。

    但我找到了解决办法。

    如果您转到右侧的 Gradle 面板。选择您想要的模块,例如。 app,然后在 build 下选择 assembleDebug 它会在标准输出中显示所有错误。

    【讨论】:

    • 赞,找到问题的好方法!
    • 这非常有用。谢谢!
    • 很棒的评论,非常有帮助,当我这样做时,我能够立即找到问题...原来我的 xml 中有一个错误的名称约束
    • 这才是真正的答案。目前唯一能找到您的 AAPT2 问题并节省时间的方法
    • 非常感谢...从昨天解决这个问题开始发疯了
    【解决方案4】:

    看看你得到的错误:

    C:\Projects\TimeTable\app\src\main\res\layout-land\activity_main.xml 错误:错误:资源 android:attr/colorSwitchThumbNormal 是私有的。

    这意味着在您的 activity_main.xml 中您引用了颜色“android:colorSwitchThumbNormal”,但在“android”命名空间内该资源是私有的。您可能想要做的是尝试从该属性的支持版本中引用该颜色,因此不要使用“android:”前缀。

    <item name="android:colorSwitchThumbNormal">@color/myColor</item>
    

    替换为:

    <item name="colorSwitchThumbNormal">@color/second</item>
    

    【讨论】:

    • 看了你的回答后我明白了错误但仍然无法解决错误.....我已经添加了android_main.Xml文件......希望你能看看在它。
    • "?android:attr/colorSwitchThumbNormal" -> "?attr/colorSwitchThumbNormal"。不要使用“android”命名空间。
    • 我遇到同样的错误,尝试更新构建工具没有成功,也删除了所有@android资源引用,但根本没有任何变化,同样的错误
    • @DaniloDeQueiroz 您确定要修改资源而不是 build/intermediates 目录中的中间文件吗?
    • 我现在解决了所有链接资源错误,项目运行并构建,但我收到了这个非常烦人的 lint 警报Error: All com.android.support libraries must use the exact same version specification (mixing versions can lead to runtime crashes). Found versions 27.1.1, 27.1.0. Examples include com.android.support:animated-vector-drawable:27.1.1 and com.android.support:design:27.1.0
    【解决方案5】:

    在 Android Studio 终端中运行 ./gradlew build -stacktrace。它可以帮助您找到导致此错误的文件。

    【讨论】:

      【解决方案6】:

      很明显,如果您有一个大项目,检查每个 xml 文件将是一场噩梦。

      在 android studio 终端中运行以下命令,它将为您提供包含问题的文件名

      gradlew build --stacktrace
      

      【讨论】:

        【解决方案7】:

        此消息表示您的 XML 文件中存在语法错误。 如果 Logcat 无法使用带有 xml 文件名的友好日志消息来查明错误,请尝试清理项目并重建项目。它对我有用。

        在 Build 选项卡中,您将获得一个名为 Android Issues 的节点。

        解释XML文件错误的错误消息在它下面。

        【讨论】:

          【解决方案8】:

          @P Fuster 提供的答案更笼统。 您的 xml 文件中可能有错误。

          我遇到了同样的错误,并且在缺少结束标记的可绘制对象中出现错误。

          【讨论】:

            【解决方案9】:

            我知道这个问题已经得到解答,但是,我仍然想补充一下这里发布的很多内容,以供其他有类似但不具体问题的人使用。

            此错误有时也会出现“Android 资源链接失败”。它有时是模糊的。解决方案有时可能非常无聊和痛苦,但是这个错误绝对不是什么严重的问题。

            在这种情况下,错误不是 Vague,因为 Android Studio 指向了问题文件。

            一般原因 当 Gradle 在您的一个或多个 XML 文件中遇到错误(通常是语法或拼写错误)时,就会显示该错误。

            大部分 XML 文件都位于 res 文件夹中,但是,您可能仍会在 res 文件夹之外找到一些文件。例如,位于“/src/main/AndroidManifest.xml”中的 AndroidManifest.xml。这是一个布局文件示例,它会给出“错误:链接文件资源失败”错误。

            解决方案 要解决此问题,您所要做的就是检查每个 XML 文件以找出错误可能出在哪里。这有时会很耗时,但请放心,一旦您找到并修复问题 XML,错误就会消失。

            你可能很幸运,Android Studio 会像问题中所问的那样为你指出确切的方向......

            其他时候,例如当您使用旧版本的 Android Studio 时,错误不会是这个特定的。

            这里有一些快速找到问题 XML 的提示 如果您有很多 XML 文件,并且一个接一个地浏览它们太困难了,您可以使用以下提示来简化该过程:

            撤消:大多数情况下,在遇到此问题之前,您已经完成了一个项目。这意味着最近对导致问题的 XML 文件之一进行了更改。如果是这种情况,您可以尝试撤消最近对 XML 文件所做的更改。要撤消,只需使用组合键:Ctrl+Z。

            打开每个 XML 文件: 第二种方法只需打开项目中的每个 XML 文件(无需扫描)。原因是有时,在打开问题 XML 文件后,Android Studio 会检测到错误并在准确的行下划线。

            【讨论】:

              【解决方案10】:

              这是一个 xml 错误,出于某种原因可能是因为删除了另一个视图中的视图或字符串,或者可能是因为缺少 " 或 /> ...。 .等

              但在这里,我正在使用 好技术 来找出问题到底出在哪里:-

              • 转到每个 xml 文件最小化如果你发现这样的东西 所以你知道错误在哪里,修复它然后对所有文件重复这个过程。

              • 如果文件有错误,您会看到三个红色点
              • 修复错误这里我删除了这一行,因为这个数组不再在字符串文件中。
              • 如果您仍然遇到主要错误,则现在您可以看到此文件中没有错误,因此您必须再次对所有 xml 文件重复此过程,直到您解决错误

              我知道这不是主要问题的解决方案,但它会让您快速找到错误所在并节省您的时间。

              更新

              还有一种方法你可以很容易找到,就像Bennik2000在评论中写的那样,你可以查看右侧滚动条是否有红线,它们也表示错误。

              更新

              我找到了一种非常简单的方法来准确获取错误在资源文件甚至 java 文件中的位置。
              1- 转到项目窗口。
              2- 打开有错误的文件。
              3-点击F2去最近的错误出现(它会去正确的行)

              【讨论】:

              • 您也可以查看右侧滚动条的红线,它们也表示错误
              【解决方案11】:

              我遇到了同样的问题。这是由于我的一个 XML 文件中的一个属性中的拼写错误。无论如何,Android Studio 并没有在第一次运行时指定错误的来源。但是当我做了一个

              构建 -> 清理项目

              然后再次运行该应用程序,它向我显示了 XML 代码中错字所在的确切行。

              【讨论】:

              • 我只有 Make Project,我只是 CTRL + Z 来跟踪导致该错误的原因。
              【解决方案12】:

              错误与 .xml 文件的某些问题有关。手动打开每个 xml 格式文件以检查错误。我有同样的问题。不得不手动打开每个文件。 @string 调用出错。

              【讨论】:

                【解决方案13】:

                我有这个问题。主要问题是我在xml 文件中向BottomAppBar 添加了一个属性。我在android 底部构建部分中发现错误消息解释说错误我替换了属性并且一切正常。对于其他有相同错误但我的解决方案对他们不满意的人,您必须阅读错误消息,它将引导您解决痛点。

                【讨论】:

                  【解决方案14】:

                  检查您的 XML 文件以解决错误。大多数情况下,对您的 XML 文件所做的更改会受到影响。因此请尝试检查您所做的最后更改。

                  尝试清理您的项目....它有效

                  快乐编码:)

                  【讨论】:

                  • 这也可能是评论而不是答案。请阅读规则。
                  【解决方案15】:

                  这可能对正在寻找不同答案的人有用。转到 Gradle 面板 并选择您的 module -&gt; Task -&gt; Verification -&gt; Check

                  这将检查项目是否有错误,并将打印发生错误的日​​志。大多数情况下,这种错误必须是项目的XML 文件中存在的拼写错误

                  【讨论】:

                    【解决方案16】:

                    如果您在 XML 布局文件中留下具有 null 或空属性的元素,或者您的 java 文件的对象创建路径(例如为对象指定不正确的 ID),也会发生这种情况

                    这里frombottom= AnimationUtils.loadAnimation(this,R.anim); 在哪个动画中。 id 或文件名留空会导致此类问题。

                    【讨论】:

                      【解决方案17】:

                      我正在做 Temperatur Converter App。我在运行该应用程序时遇到了同样的错误: Android Studio 链接失败。 在 String.xml 中遗漏了一行,我包含了该行。一切顺利,没有任何错误。

                      修正前

                      <resources>
                      <color name="myColor">#FFE4E1</color>
                      <string name="Celsius">To Celsius</string>
                      <string name="fahrenheit">To Fahrenheit</string>
                      <string name="calc">Calculate</string>
                      </resources>
                      

                      编辑后:

                      <resources>
                      <string name="app_name">Temp Converter</string>
                      <color name="myColor">#FFE4E1</color>
                      <string name="Celsius">To Celsius</string>
                      <string name="fahrenheit">To Fahrenheit</string>
                      <string name="calc">Calculate</string>
                      </resources>
                      

                      【讨论】:

                        【解决方案18】:

                        有时会发生你从另一个项目中复制粘贴代码的情况

                        <android.support.design.widget.NavigationView
                                android:id="@+id/nav_view"
                                android:layout_width="256dp"
                                android:layout_height="match_parent"
                                android:layout_gravity="start"
                                android:background="@android:color/white"
                                android:fitsSystemWindows="true"
                                app:headerLayout="@layout/nav_header_main"
                                app:itemTextColor="@color/colorDrawerItems"
                                app:menu="@menu/activity_main_drawer" />
                        

                        不幸的是,Android Studio 并不总是显示依赖错误,但是由于 implementation 'com.android.support:design 未添加到项目中,因此缺少 NavigationView

                        【讨论】:

                          【解决方案19】:

                          如果您之前删除了 Activity java 文件并且没有删除相应的 xml 布局文件,也可能会出现此类似错误。

                          删除已删除的Activity java文件的xml文件并清理或重建项目。

                          其背后的原因可能是因为xml中的上下文不匹配。 (如果您正在复制粘贴 xml 布局代码,则可能会发生这种情况) 将 tools:context 与 Activity 的 java 文件匹配。

                          【讨论】:

                            【解决方案20】:

                            当您有一个随机 XML 文件什么都不做时,有时会发生这种情况。删除文件可以解决问题。

                            【讨论】:

                            • 如果文件是正确的和格式化的,它是否存在并不重要。
                            【解决方案21】:

                            就我而言,我制作了一个无法识别的自定义背景。

                            我从这两个 XML 资源文件的顶部删除了 &lt;?xml version="1.0" encoding="utf-8"?&gt; 标记。

                            在尝试了社区的许多解决方案后,这对我有用。 @P Fuster 的回答让我尝试了这个。

                            【讨论】:

                              【解决方案22】:

                              -问题可能是您删除了 .java 文件,这样做不会删除 .XML 文件,因此请转到 res-> 布局并删除您之前删除的那些 .XML 文件。 - 另一个问题可能是您没有根据您最近删除的语法删除清单中存在的文件... 所以删除并运行代码

                              【讨论】:

                                【解决方案23】:

                                我有同样的问题,但它出现在我所有的项目中,我尝试了Invalidate Cache / Restart,但即使它也没有解决问题。

                                最后我意识到在我的Gradle Settings 中,Offline Work 已启用。

                                转到Build, Execution, Deployment > Gradle in Gradle Settings 未选中 Offline Work

                                它解决了为我的 Android Studio 下载一些配置文件的问题。

                                【讨论】:

                                  【解决方案24】:

                                  我的错误出现在 xml 可绘制文件中。我有第一个班轮副本。将其更改为对我有用:

                                  <?xml version="1.0" encoding="utf-8"?>
                                  

                                  【讨论】:

                                    【解决方案25】:

                                    添加您已删除的可绘制图像 .jpg/png 等格式。 和 然后运行您的项目以在 android studio 3.6.1 上正常工作

                                    【讨论】:

                                      【解决方案26】:

                                      我收到此错误是因为 2 个内容相同但大小写不同的行

                                          app:layout_constraintHorizontal_bias="0.5"
                                          ...
                                          app:layout_constrainthorizontal_bias="0.5"
                                      

                                      Android Studio 2020.3.1 并没有在 xml 文件中将此标记为错误,而是在尝试构建时会抛出资源链接错误

                                      【讨论】:

                                        猜你喜欢
                                        • 1970-01-01
                                        • 1970-01-01
                                        • 2019-02-01
                                        • 2022-07-07
                                        • 1970-01-01
                                        • 1970-01-01
                                        • 2019-03-09
                                        • 1970-01-01
                                        相关资源
                                        最近更新 更多