【问题标题】:Android image can't convert to drawableAndroid图像无法转换为drawable
【发布时间】:2016-04-21 19:51:12
【问题描述】:

我无法添加图片。我下载了图像并将其添加到 drawable-mdpi 下的 res 目录中。然后当我尝试使用 imageView 在视图上添加图像时,它给了我这个错误

Couldn't resolve resource @drawable/ball01 
Failed to convert @drawable/ball01 into a drawable

这是我的 xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame" >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:layout_gravity="center"
        android:textSize="32sp"
        android:layout_above="@+id/button"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Enlighten Me"
        android:id="@+id/button"

        android:layout_centerVertical="true"
        android:layout_alignParentRight="true" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_above="@+id/textView"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ball01" />


</RelativeLayout>

我在这里做错了什么。

【问题讨论】:

  • 检查图像文件名并确保它与您在@drawable\...中指定的名称匹配或检查图像的扩展名并确保它是jpg,png,jpeg...
  • 这听起来很傻,但是您是否尝试在将图像添加到您的可绘制文件夹后刷新您的项目文件夹?
  • 这就是尼拉吉。我不得不关闭 IDE 并返回...谢谢

标签: android android-layout


【解决方案1】:

目录名称应为“drawable-mdpi”。使用连字符代替下划线。

【讨论】:

  • 抱歉打错了..实际上是drawable-mdpi
  • 所以我还在寻找解决方案
【解决方案2】:

图片名称必须为数字或小写。ide仅标识。

【讨论】:

    【解决方案3】:

    我遇到了同样的问题,我有一张我命名为 Barcode 的图像,然后将其保存到我的可绘制文件夹中,然后我决定将其命名为带有小 b 的条形码,因此我将其重命名。 然后我检查了它是否可以工作,我得到了和你一样的错误,在我的 xml 中将名称更改为带有大 B 的“条形码”后,它工作了。

    我的解决方案:从 drawable 中删除条形码图像,然后用我将要使用的名称再次添加它,不要在 drawable 文件夹中更改名称。

    【讨论】:

      【解决方案4】:

      我在 android studio 上遇到过这个问题。 如果您关闭并打开项目并且它继续,您需要做的就是右键单击您放在“res”文件夹中的特定图像(这就是问题所在)并重构-RENAME。删除文件名中的空格。应该这样做。

      【讨论】: