android - 错误 :error: 'drawable/delete.png' is incompatible with attribute android:background (attr) reference|color

标签 android xml android-layout

当我尝试更改 Activity 的 XML 文件中的 android:background 时遇到了这个问题。

Information:Gradle tasks [:app:assembleDebug]
C:\Users\Valentin\AndroidStudioProjects\CedarRestaurants4\CedarRestaurants3\app\src\main\res\layout\activity_dynamic_view_ordering.xml
Error:error: 'drawable/delete' is incompatible with attribute android:background (attr) reference|color.
Error:'drawable/delete' is incompatible with attribute android:background (attr) reference|color.
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 4s
Information:7 errors
Information:0 warnings
Information:See complete output in console

我的 Activity 的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent_linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:orientation="vertical" >
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal" >
        <EditText
            android:id="@+id/number_edit_text"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="5" />

        <Button
            android:id="@+id/delete_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="drawable/delete.png"
            android:onClick="onDelete" />
    </LinearLayout>
    <Button
        android:id="@+id/add_field_button"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="#555"
        android:layout_gravity="center"
        android:onClick="onAddField"
        android:textColor="#FFF"
        android:text="New"
        android:paddingLeft="5dp"/>
</LinearLayout>

<Button
    android:id="@+id/placeOrderButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/parent_linear_layout"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="250sp"
    android:text="@string/place_order"
    android:textSize="32sp" />


</RelativeLayout>

我从教程项目中复制了 Activity 布局文件和 Activity ,并尝试将图像资源更改为更现代的东西。

我已经确保我的 delete.png 位于 /src/main/res/drawable 中,并且已经尝试在最后删除 .png,但没有运气。感谢您的帮助。

最佳答案

您忘记将 @ 添加到您的 drawable 并删除 extention。所以改变你的

android:background="drawable/delete.png"

这个

android:src="@drawable/delete"

关于android - 错误 :error: 'drawable/delete.png' is incompatible with attribute android:background (attr) reference|color,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54880183/

相关文章:

android - 我应该有 2 项 Activity ,一项用于查看文本,一项用于编辑,还是反之亦然?

java - TextView 和 ImageView 脱离 LinearLayout

Android项目+Eclipse+注解处理

python - 在其他字段发生变化时更改字段值 OpenERP/Python

android - 有没有一种简单的方法可以用 ArrayList 填充 Spinner?

xml - 如何验证兼容的 XML 站点地图?

python - 尝试使用 Beautiful Soup 或 ElementTree 从链接的迭代列表中抓取信息

android - 在android上设置计算器按钮的布局

Android 背景在按下时呈波纹状,但在选择时呈纯色?

java - 你如何使用android sdk发送短信