在包''中找不到属性'srcCompat'的资源标识符。'

问题描述:

我会说我已经在*上看到了这个问题的一些答案,但只提供了一个快速'修复'的解决方案。如果可能的话,我希望能够理解为什么会发生这种情况(有一些详细的答案)。在包''中找不到属性'srcCompat'的资源标识符。'

现在来回答这个问题:我刚刚使用Android Studio 2.2重新开始了Android开发。我有一个ios应用程序,我想移植到Android(意思是,为Android重新创建它)。我已经开始使用Android Studio中的基本模板,添加了一个约束布局和2个使用2个PNG文件的ImageViews,这些文件已复制到DRAWABLE文件夹中。

而不进行任何更改或任何,当我尝试建立我得到这个错误:

错误:(11)未找到属性资源标识符包“X.Y.Z” srcCompat“。这发生在两个图像上。下面是布局文件:

<?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/lib/x.y.z" 
xmlns:app2="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" 
tools:context="x.y.z.MainActivity"> 

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    app2:srcCompat="@drawable/pngFile1" 
    android:id="@+id/imageView" 
    app2:layout_constraintBottom_toBottomOf="parent" 
    android:layout_marginEnd="8dp" 
    app2:layout_constraintRight_toRightOf="parent" 
    android:layout_marginBottom="8dp" 
    android:layout_marginRight="8dp" /> 

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    app2:srcCompat="@drawable/pngFile2" 
    android:id="@+id/imageView2" 
    app2:layout_constraintBottom_toTopOf="@+id/imageView" 
    app2:layout_constraintRight_toRightOf="@+id/imageView" 
    android:layout_marginBottom="8dp" /> 

现在,这里有一些事情,我想明白了,才能有一个正确回答我的问题:

  • 一些我找到的其他答案建议更改默认值:

    xmlns:app =“http://schemas.android.com/apk/res-auto”

到:

xmlns:app="http://schemas.android.com/apk/lib/x.y.z" 

这是真的,这从构建删除错误,但似乎什么奇怪,我(和我的小到没有Android体验),是Android Studio中创建然后又类似的线当我添加第二个图像:

xmlns:app2="http://schemas.android.com/apk/res-auto" 

而且我们可以看到,它使用本作的画面,所以错误再次出现。

我记得就像一年前,我曾在Android Studio上开发过一个应用程序,并且使用几乎相同的方式添加图像/按钮和图像,但这些问题并不存在。

这就是为什么我还想了解为什么会发生这种情况以及如何正确解决问题。

在此先感谢!

+0

我有一个项目,我已经工作了上一年多参考此答案,并且添加图像总是正常并且正常。最近我发现了“约束布局”,所以我开始使用,我放入了一些imageViews,然后就像你提到的一样。这里的区别是“srcCompat” - 为什么它改变了?似乎更新App.Compat到最新版本可以解决这个问题,我还没有做到这一点,因为我有一些依赖于以前的版本。 –

+0

在这里看看它可能会给你一些背景 - 我们使用约束布局的预览版本,可以解释一些奇怪的行为https://sites.google.com/a/android.com/tools/recent –

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/imageView" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentEnd="true" 
    android:background="@drawable/cooltext201199220690445" /> 

我只是改变了我的成这样,它的工作对我来说 我刚才删除了srccompat与“背景” 取代它,我彻底删除“的xmlns:APP 2 =” http://schemas.android .COM/APK/RES-AUTO”希望这可以帮助您:)

+0

它为我工作,而不必删除xmlns:应用程序部分。为什么会发生这种错误?它是生成应用程序的Android Studio:srcCompat – Donato

所有你需要专注于你正在使用的Android Studio版本的拳头。你也应该看看minSdkVersiontargetSdkVersion。 尝试使用android:src=""而不是app2:srcCompat=

app2:srcCompat= suports older APIs when loading vectorDrawable. 

如果你想使用它,然后修改你的构建。gradile一样,

android { 
    defaultConfig { 
    vectorDrawables.useSupportLibrary = true 
    } 
} 

欲了解更多信息,你可以参考这个link

,或者你可以在*