布局中的布局<layout>在基本布局文件夹中没有声明[错误]

L K*_*emp 61 xml android android-studio

从Eclipse迁移到新版本的Android Studio 3.2,API 28后,我的应用程序布局出现以下错误:

布局中的布局在基本布局文件夹中没有声明; 在与此限定符不匹配的配置中查询资源时,这可能导致崩溃

我收到此错误的布局之一是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@null" >

      <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top|center_horizontal"
            android:adjustViewBounds="true"
            android:contentDescription="@string/hello_world"
            android:src="@drawable/loading_top" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center_horizontal"
            android:adjustViewBounds="true"
            android:contentDescription="@string/hello_world"
            android:src="@drawable/loading_bottom" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:contentDescription="@string/hello_world"
            android:background="@color/white"
            android:layout_marginBottom="5dp"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:src="@drawable/loading_logo" />

        <ImageView
            android:id="@+id/loading"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center|center_vertical"
            android:layout_marginBottom="0dp"
            android:layout_marginTop="0dp"
            android:contentDescription="@string/hello_world"
            android:scaleType="fitXY"
            android:src="@null" />
    </FrameLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我在第一个LinearLayout的第一行得到错误.

有谁知道如何解决这个错误?

谢谢

更新:这似乎是一个编码问题.我使用Windows资源管理器复制了文件夹中的xml文件.删除文件并在Android Studio中处理它们后,问题已经解决.

vov*_*ost 69

在我的情况下,关闭和重新开放Android工作室解决了这个问题.重建项目或使缓存无效没有帮助.

  • 请注意,“构建”&gt;“清理项目”为我解决了这个问题。 (4认同)
  • 每次添加新的布局文件时我都会看到这一点。重新启动/重建修复了它,但有人找到它的根本原因吗? (3认同)

Gui*_*ira 32

无效缓存/重新启动解决了我的问题。


Ito*_*oun 30

你也可以试试这个:

File => Invalidate Caches/Restart => Invalidate和Restart.

截图

  • 是的,它有帮助,但很奇怪,在以前的版本中,Build> Rebuild Project有帮助.他们撞坏了什么东西. (2认同)

小智 15

就我而言,它是在文件名中使用大写字母。我将所有内容都保留为小写,并且已修复。


rmi*_*lle 9

我已经做了 Build > Clean Project


Tra*_*llo 8

在 Gradle 模块之间移动布局文件时收到此错误。我不必使缓存失效,因为它在我重新启动Android Studio清理项目时自行解决。这似乎是一个IDE 错误


小智 6

就我而言,我解决了这个问题

Android 告诉我这个id-error: MissingDefaultResource作为文档说:

如果资源仅在带有 -land 或 -en 等限定符的文件夹中定义,并且基本文件夹(布局或值等)中没有默认声明,则如果在设备所在的设备上访问该资源,则应用程序将崩溃处于缺少给定限定符的配置中。

作为一种特殊情况,不必在基本文件夹中指定可绘制对象;如果密度文件夹(例如 drawable-mdpi)中存在匹配项,则将使用并缩放该图像。但是请注意,如果您只在像 drawable-en-hdpi 这样的文件夹中指定可绘制对象,则应用程序将在非英语语言环境中崩溃。

在某些情况下,您可能拥有一个资源,例如 -fr drawable,它仅从具有相同限定符(例如 -fr 样式)的其他资源中引用,而该资源本身具有安全回退。但是,这仍然可能让某人意外引用可绘制对象并导致崩溃,因此在基本文件夹中创建默认的虚拟回退更安全。或者,您可以通过在元素上添加 tools:ignore="MissingDefaultResource" 来抑制该问题。

(这种情况经常发生在字符串翻译中,您可能会删除代码和相应的资源,但忘记删除翻译。该场景有一个专用的问题 ID,其 ID 为 ExtraTranslation。)

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      tools:ignore="MissingDefaultResource" <!--this-->
      android:layout_width="match_parent"
      android:layout_height="match_parent">
Run Code Online (Sandbox Code Playgroud)

但这是一个简单的解决方案,因为你会错过 Android 中的资源。

我用文件系统选项解决了这个问题。如果Android告诉您缺少资源意味着缺少某些东西,您应该补充它。

你应该去\app\src\main\res

也许:右键单击文件丢失的文件夹,在Exporer中显示,然后返回\app\src\main\res

你应该找到

/layout

/layout-normal

/layout-large

/some more folders
Run Code Online (Sandbox Code Playgroud)

您应该找到文件丢失的位置。在我的情况下, /layout 中缺少


L K*_*emp 6

我已使用 Windows 资源管理器将 xml 文件复制到该文件夹​​中。这似乎导致了编码问题。删除文件并在Android Studio中复制后,问题解决。


Gop*_*ena 6

按-“ctrl+A”-->“ctrl+x”-->“ctrl+v”)这不会改变任何内容,但显然解决了错误。


Anb*_*oub 5

如果您设计布局:layout-sw320dp layout-sw480dp layout-sw600dp layout-sw720dp

您应该像这样默认添加布局:

在此处输入图片说明


小智 5

就我而言,这个问题是由大写字母引起的。“-”等错误字符也可能导致该问题。我将名称从 更改为Activity_disease.xmlactivity_disease.xml解决了问题。


小智 5

对我来说,发生这种情况是因为我不小心用空格键而不是下划线(即而fragment_item summary.xml不是fragment_item_summary.xml.