简体   繁体   English

资源不是可绘制的

[英]Resource is not a Drawable

This code causes force close exception on Emulator(API 18) and works in ASUS Nexus 7(API 21) 此代码在Emulator(API 18)上导致强制关闭异常,并在ASUS Nexus 7(API 21)中工作

<View
    android:layout_width="400dp"
    android:layout_height="2dp"
    android:layout_marginTop="30dp"
    android:background="@color/light_gray" />

If I am replacing @color/light_gray with #EBEBEB works perfectly on both device. 如果我用@color/light_gray替换@color/light_gray#EBEBEB在两种设备上均可正常使用。

Exception is 例外是

Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f080060 a=-1 r=0x7f080060}

So I moved to drawable on following code, 因此,我将以下代码移到了drawable上,

<Button
    android:layout_width="wrap_content"
    android:layout_marginTop="30dp"
    android:layout_height="wrap_content"
    android:ems="10"
    android:background="@drawable/login_btn_selector"
    android:text="Login"
    android:id="@+id/btnLogin" />

This one throws following exception, 这个引发以下异常,

 Caused by: android.content.res.Resources$NotFoundException: File res/drawable-hdpi-v4/login_btn_selected.xml from drawable resource ID #0x7f020095

So I tried to place the login_btn_selected.xml in res/drawable-hdpi-v4/ folder, Then it says duplicate file occur between res/drawable-hdpi/ and res/drawable-hdpi-v4/ , So I removed it from res/drawable-hdpi/ , Then again same resource not found exception in v4 folder, 所以我试图将login_btn_selected.xml放在res/drawable-hdpi-v4/文件夹中,然后说在res/drawable-hdpi/res/drawable-hdpi-v4/之间出现重复文件,所以我从res/drawable-hdpi/删除了它。 res/drawable-hdpi/ ,然后再次在v4文件夹中找不到相同的资源异常,

Caused by: android.content.res.Resources$NotFoundException: File res/drawable-hdpi-v4/login_btn_selected.xml from drawable resource ID #0x7f020095

So finally I came to SO . 所以最后我来到了SO

The problem is, 问题是,

  1. I want to know is this problem in emulator only or in real device too. 我想知道是仅在模拟器中还是在实际设备中都存在此问题。
  2. If in real device too means how can I overcome this? 如果在真实设备中也意味着我该如何克服呢?

    I want to use @color/light_gray instead of hard coding, Also I want to use drawable selectors. 我想使用@color/light_gray而不是硬编码,而且我想使用可绘制的选择器。

NOTE: I have deleted and recreated the Emulator, no use. 注意:我已经删除并重新创建了模拟器,没有用。 I have tested on API 19 emulator also, same issue. 我也在API 19模拟器上测试过,同样的问题。

Any solution will be highly appreciable. 任何解决方案都是高度可取的。

It is because my color resource file is under \\res\\values-21 folder, Now moved it to \\res\\values\\ folder, Now app works fine. 这是因为我的色彩资源文件位于\\res\\values-21文件夹下,现在将其移至\\res\\values\\文件夹下,现在应用程序运行正常。 thank you friends. 谢谢朋友们。

Also make sure you don't have the background of an imageView set to selectableItemBackgroundBorderless , otherwise older devices will have this error: 还要确保您没有将imageView的背景设置为selectableItemBackgroundBorderless ,否则旧设备将出现此错误:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="?android:selectableItemBackgroundBorderless" />

I was struck with the same problem in the recent app I made. 我在最近制作的应用中遇到了同样的问题。 In my case, the problem was I put an image in the folder called drawable-v21, which is not available in older android API. 就我而言,问题是我将图像放在名为drawable-v21的文件夹中,该图像在较旧的android API中不可用。

The solution is to put your drawable in drawable-...dpi folders too. 解决方案是将可绘制对象也放置在drawable -... dpi文件夹中。

Your color resource must reside in an Android resource file (ie: in the colors.xml file). 您的颜色资源必须位于Android资源文件中(即: colors.xml文件中)。
Android would not complain where you set it, but it must be defined as a color resource . Android不会抱怨您在哪里设置它,但必须将其定义为颜色资源

Ie: 即:

<color name="light_gray">#ebebeb</color>

[EDIT] [编辑]

The names appear not to match... 名称似乎不匹配...
See this line: 看到这一行:

android:background="@drawable/login_btn_selector"

Then you say: So I tried to place the login_btn_selected.xml . 然后您说: So I tried to place the login_btn_selected.xml

It seems to be the cause of 这似乎是导致

Caused by: android.content.res.Resources$NotFoundException: File res/drawable-hdpi-v4/login_btn_selected.xml from drawable resource ID #0x7f020095.

I think the best way of doing this is, first create a xml file inside the "drawable" folder and do something like below. 我认为最好的方法是,首先在“ drawable”文件夹中创建一个xml文件,然后执行以下操作。

eg light_gray.xml then do something like below. 例如light_gray.xml然后执行以下操作。

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <solid android:color="@color/light_gray" />
    </shape>

once you have saved it. 保存后。

change you code like below. 如下更改您的代码。

<View
    android:layout_width="400dp"
    android:layout_height="2dp"
    android:layout_marginTop="30dp"
    android:background="@ drawable/light_gray" />

But make sure to put the file light_gray.xml only to "drawable" folder. 但是请确保仅将文件light_gray.xml放入“ drawable”文件夹。 Do not put the same file to "drawable-hdpi or drawable-hdpi-v4 and so on" 请勿将同一文件放入“ drawable-hdpi或drawable-hdpi-v4等”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM