链接文件资源失败

73

这是导致错误的 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 错误:error: resource android:attr/colorSwitchThumbNormal 是私有的。 错误:resource android:attr/colorSwitchThumbNormal 是私有的。 C:\Projects\TimeTable\app\src\main\res\layout-land\activity_main.xml 错误:error: resource android:attr/colorSwitchThumbNormal 是私有的。 错误:resource android:attr/colorSwitchThumbNormal 是私有的。 错误:无法链接文件资源。 错误:java.util.concurrent.ExecutionException: java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details 错误:java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details 错误:com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details 错误:执行任务 ':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>
26个回答

2
检查你的XML文件以解决错误。大多数情况下,对XML文件所做的更改会受到影响。因此,请尝试检查最近所做的更改。
尝试清理项目...它会起作用。
编码愉快 :)

这可能也是一条评论而不是答案。请阅读规则。 - dpapadopoulos

2
我曾经遇到过同样的问题。这是由于我XML文件中的一个属性拼写错误导致的。无论如何,Android Studio在第一次运行时没有指定错误来源。但是,当我执行“Build -> Clean Project”并再次运行应用程序时,它向我显示了XML代码中拼写错误的确切行。"最初的回答"

我只是个“项目制造机”,我只需按CTRL + Z来追踪导致错误的原因。 - Bay

2
我知道这个问题已经有人回答了,但我仍然想为其他遇到类似但不是特定问题的人添加一些内容。
有时这个错误也会出现为“Android资源链接失败”。它有时候很模糊。解决方案可能会相当无聊和痛苦,但是这个错误绝对没有什么严重的问题。
在这种情况下,错误并不模糊,因为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会检测到错误并在确切的行下划线。

1
我遇到了这个问题。主要问题是我在xml文件中向BottomAppBar添加了一个属性。我在android底部构建部分找到了错误信息,解释了错误,然后我替换了属性,一切正常了。对于其他遇到相同错误但我的解决方案不适用的人,你需要阅读错误信息,它将指导你找到痛点。

1
在我的情况下,我制作了一个自定义背景,但它没有被识别。
我从这两个XML资源文件的顶部删除了<?xml version="1.0" encoding="utf-8"?> 标签。
在尝试了社区中的许多解决方案后,这对我起作用了。@P Fuster的答案促使我尝试这个方法。

1
这可能对正在寻找不同答案的人有用。进入Gradle面板,选择您的模块 -> 任务 -> 验证 -> 检查
这将检查项目中的错误,并打印错误发生的日志。大多数情况下,这种错误必须是项目的XML文件中存在的拼写错误。

0

我因为两个内容相同但大小写不同的行而出现了这个错误:

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

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


0

我的错误在一个xml drawable文件中。我有第一行重复了。将其更改为以下内容对我有用:

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

0

我曾经遇到过同样的问题,但它发生在我所有的项目中。我尝试了无效缓存/重启,但它并没有解决问题。

最后我意识到,在我的Gradle设置中,离线工作被启用了。

进入构建、执行、部署>Gradle,在Gradle设置中取消选中离线工作

enter image description here

它解决了我在Android Studio下载某些配置文件时遇到的问题。


0

如果您在XML布局文件中留下具有空属性的元素,或者在Java文件中创建对象的路径(例如为对象指定不正确的ID),也可能会发生此问题。

enter image description here

在编程中,frombottom= AnimationUtils.loadAnimation(this,R.anim); 中的 anim 参数留空可能会导致此类问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接