链接文件资源失败

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个回答

130

如果您的java文件中出现了错误,可能是因为有一个或多个XML文件存在错误。

检查所有XML文件并解决错误,然后从生成菜单中进行清理(clean)重建(rebuild)项目。

从最近编辑的XML文件开始处理。


1
有时候问题可以通过清理或重建项目来解决。但并不是每次都有效,应该检查最近添加的文件。 祝学习愉快.. - Arbaz.in
3
我在 XML 文件中遇到了问题,但我的 IDE 没有进行高亮显示。我不得不请求同事为我编译,结果在他的 IDE 中出现了。Android Studio 有 bug。 - arenaq
在大多数情况下,这个错误是由于drawable/xml文件引起的。 - Harvinder Singh
只需使用CTRL + Z来执行在XML页面上所做的操作,以跟踪/检测导致此错误的原因。 - Bay
可能是拼写错误导致的问题,例如layout_constraintEnd_toEndtOf而不是layout_constraintEnd_toEndOf - Victor Oliveira
显示剩余5条评论

37

如果有人在阅读此文时遇到同样的问题,我最近也遇到了这个问题,原因是不小心将xml头写了两次:

<?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文件不会有类似这样的错误(因为它不会显示为错误)。编辑现在看起来像是一个错误,确保检查你的错误日志。


谢谢,我有最近创建的文件的问题。 - Arbaz.in
修复已到位..! - Anu Martin
我由于没有XML头部也遇到了相同的错误。你的评论让我找到了问题所在。 - lyndon hughey

35

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

我在XML文件中遇到了一个问题,但我的IDE没有突出显示。我不得不请同事为我编译,然后在他的IDE中显示。是Android Studio有bug。

但我找到了一个解决方法。

如果您转到右侧的Gradle面板。选择所需的模块,例如app,然后在build下选择assembleDebug,它将在stdout中显示所有错误。

输入图像描述


1
太棒了,找到问题的好方法! - Kanagalingam
1
这是真正的答案。目前找到您的AAPT2问题并节省时间的唯一方法。 - Deepak kaku
非常感谢...昨天一直在解决这个问题,差点把自己逼疯了。 - Rahul Kumar
这绝对是处理AAPT问题的最佳方式。 - Michael
我们已经等待了这么久,终于等到了它。向我们的救世主致敬。 - omer.ersoy
显示剩余3条评论

13

看看你收到的错误:

C:\Projects\TimeTable\app\src\main\res\layout-land\activity_main.xml 错误:error: 资源 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文件.....希望你能看一下。 - Daksh Barad
"?android:attr/colorSwitchThumbNormal" -> "?attr/colorSwitchThumbNormal"。不要使用“android”命名空间。 - Izabela Orlowska
我一直在遇到同样的错误,尝试更新构建工具但没有成功,也删除了所有@android资源引用,但是没有任何变化,仍然是同样的错误。 - DaniloDeQueiroz
@DaniloDeQueiroz 你确定你修改的是资源文件而不是 build/intermediates 目录下的中间文件吗? - Izabela Orlowska
我暂时解决了所有的链接资源错误,项目可以运行和构建,但是我一直收到这个非常烦人的lint警告:“错误:所有com.android.support库必须使用完全相同的版本规范(混合版本可能导致运行时崩溃)。发现版本27.1.1、27.1.0。例如,com.android.support:animated-vector-drawable:27.1.1和com.android.support:design:27.1.0。” - DaniloDeQueiroz
我刚刚降级了支持版本,错误停止了,但是依赖项的依赖项有不同的版本,这很奇怪。 - DaniloDeQueiroz

8
在Android Studio终端中运行./gradlew build -stacktrace。它可以帮助你找到引起这个错误的文件。

5

如果你有一个大项目,显然检查每个xml文件会是一场噩梦。

在Android Studio终端中运行以下命令,它将给出存在问题的文件名:

gradlew build --stacktrace

enter image description here


3

这条信息表示你的XML文件中存在语法错误。如果Logcat无法通过友好的日志消息和xml文件名称确定错误位置,请尝试清理项目并重新构建项目。这种方法对我有用。

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

XML文件中的错误信息将显示在该节点下方。


2

关于@P Fuster提供的答案,更加通俗易懂地说,您的xml文件可能存在错误。

我遇到了同样的错误,并且在绘图中缺少结束标签。


2

错误与 .xml 文件的某些问题相关。手动打开每个 xml 格式文件以检查错误。我也遇到了同样的问题,不得不手动打开每个文件。@string 调用中出现了错误。


2

由于删除了另一个视图中的视图或字符串,或者缺少“或/&gt;......等”,出现了XML错误

但是,我使用了一种好的技巧来确定问题所在:

  • 转到每个单独的XML文件最小化根元素,如果您发现类似以下内容的内容,则可以找到错误所在,然后修复它,然后为所有文件重复此过程。

  • 如果文件有错误,您将看到三个带红色点的点 enter image description here
  • 修复错误,在此处我删除了此行,因为此数组不再在字符串文件中。 enter image description here
  • 现在,您可以看到此文件中不再存在任何错误,如果仍然存在主要错误,则必须再次针对所有XML文件重复此过程,直到解决错误 enter image description here

我知道这不是解决主要问题的方法,但它可以帮助您快速找到错误并节省时间。

更新

还有另一种方法可以轻松找到错误,正如Bennik2000在评论中所写的那样,您可以查看右侧滚动条以获取红色线条,它们也表示错误。

更新

我发现了一种非常简单的方法,可以确定资源文件或甚至Java文件中的错误位置。
1- 转到项目窗口。
2- 打开具有错误的文件。
3- 单击F2以跳转到最接近的错误(它将转到正确的行)


enter image description here


1
同时,您可以查看右侧滚动条上的红线,它们也表示错误。 - Bennik2000

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