链接文件资源失败

Failed linking file resources

这是出现错误的 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]);
            }
       });
   }
}

我得到的错误如下

Information:Gradle tasks [clean, :app:assembleDebug] C:\Projects\TimeTable\app\src\main\res\layout\activity_main.xml Error:error: resource android:attr/colorSwitchThumbNormal is private. Error:resource android:attr/colorSwitchThumbNormal is private. C:\Projects\TimeTable\app\src\main\res\layout-land\activity_main.xml Error:error: resource android:attr/colorSwitchThumbNormal is private. Error:resource android:attr/colorSwitchThumbNormal is private. Error:failed linking file resources. Error:java.util.concurrent.ExecutionException: java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details Error:java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details Error:com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details Error:Execution failed for task ':app:processDebugResources'. Failed to execute aapt Information:BUILD FAILED in 9s Information:9 errors Information:0 warnings Information:See complete output in console

这是 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>

查看您遇到的错误:

C:\Projects\TimeTable\app\src\main\res\layout-land\activity_main.xml Error:error: resource android:attr/colorSwitchThumbNormal is private.

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

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

替换为:

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

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

在构建选项卡中,您将获得一个名为 Android 问题的节点。

解释 XML 文件错误的错误消息在其下方。

如果读到这篇文章的人有同样的问题,我最近发生了这种情况,这是因为 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 文件都没有这样的错误(因为它不会显示为错误)。 编辑现在似乎显示为错误,请务必检查您的错误日志。

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

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

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

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

从最近编辑的 XML 文件开始

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

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

但是我在这里使用了好的技术来找出问题的确切位置:-

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

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

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

更新

还有一个方法你可以很容易地找到,正如Bennik2000在评论中写的,你可以看右边的滚动条是否有红线,它们也表示错误。

更新

我找到了一个非常简单的方法来确定错误的确切位置,如果它在资源文件甚至 java 文件中。
1- 转到项目 Window。
2- 打开有错误的文件。
3- 单击 F2 转到最近的错误出现(它将转到正确的行)

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

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

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

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

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

的 java 文件

我有这个 problem.The 主要问题是我在 xml file.I 中向 BottomAppBar 添加了一个属性,在 android 底部构建中发现了错误消息部分解释了错误,我替换了属性,一切正常。对于其他有同样错误但我的解决方案不适合他们的人,您必须阅读错误消息,它将引导您找到痛点。

我在做温度转换器 App.I 时遇到同样的错误,而 运行 应用程序如下: 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>

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

<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 并不总是显示依赖性错误,但实际情况是缺少 class NavigationView 因为 implementation 'com.android.support:design 未添加到项目中

检查您的 XML 文件以解决 Errors.Most 对您的 XML 文件所做更改的时间 Affected.so 尝试检查您所做的最后更改。

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

快乐编码:)

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

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

我从那两个 XML 资源文件的顶部删除了 <?xml version="1.0" encoding="utf-8"?> 标签。

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

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

I had an issue in XML file that IDE did not highlight. Had to ask my colleague to compile for me and it shown in his IDE. Buggy Android Studio.

但我找到了解决方法。

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

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

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

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

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

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

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

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

你可能很幸运,AndroidStudio 会为你指出问题中所问的确切方向……

其他时候,例如当您使用旧版本的 Android Studio 时,错误不会如此具体。

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

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

打开每个 XML 文件: 第二种方法涉及简单地打开项目中的每个 XML 文件(不扫描)。这样做的原因是有时,在打开有问题的 XML 文件后,Android Studio 会检测到错误并在确切的行下划线。

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

Build -> Clean Project

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

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

最后我意识到在我的 Gradle Settings 中启用了 Offline Work

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

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

我的错误出现在 xml 可绘制文件中。我有第一个班轮副本。把它改成这个对我有用:

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

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

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

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

gradlew build --stacktrace

这可能对正在寻找不同答案的人有用。转到 Gradle 面板 和 select 您的 module -> Task -> Verification -> Check.

这将检查项目是否有错误,并打印发生错误的日​​志。大多数情况下,这种错误一定是项目 XML 文件中的错字

再次添加您删除的可绘制图像。jpg/png 等格式。 和 然后 运行 您的项目可以在 android studio 3.6.1

上正常工作

由于 2 行内容相同但大小写不同:

,所以我收到了这个错误
    app:layout_constraintHorizontal_bias="0.5"
    ...
    app:layout_constrainthorizontal_bias="0.5"

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