错误:'drawable/delete.png' 与属性 android:background (attr) reference|color 不兼容

5

当我尝试在我的活动的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>

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

我已经确保我的 delete.png/src/main/res/drawable 文件夹中,已经尝试去掉末尾的 .png,但没有成功。任何帮助都将不胜感激。

2个回答

8
你忘记在你的drawable中添加@并删除拓展名。所以请修改:
android:background="drawable/delete.png"

转化成这个

android:src="@drawable/delete"

我最终只是把android:background="@drawable/delete"改了一下,然后它完美地运行了。谢谢! - defkey123

0
<?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"
            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>

1
一个解释可能非常有用。 - Riad Rekab

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