java 资源不是可绘制的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28915699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Resource is not a Drawable
提问by Gunaseelan
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
与#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
in res/drawable-hdpi-v4/
文件夹放入,然后它说在res/drawable-hdpi/
和之间出现重复文件res/drawable-hdpi-v4/
,所以我将其从 中删除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,
问题是,
- I want to know is this problem in emulator only or in real device too.
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.
任何解决方案都将是非常可观的。
回答by Gunaseelan
It is because my color resource file is under \res\values-21
folder, Now moved it to \res\values\
folder, Now app works fine. thank you friends.
这是因为我的颜色资源文件在\res\values-21
文件夹下,现在将它移到\res\values\
文件夹中,现在应用程序工作正常。谢谢朋友。
回答by IHeartAndroid
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" />
回答by akhil
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 也放在 drawable-...dpi 文件夹中。
回答by Phant?maxx
Your color resource must reside in an Android resource file (i.e.: in the colors.xml
file).
Android would not complain whereyou set it, but it mustbe defined as a color resource.
您的颜色资源必须驻留在 Android 资源文件中(即:在colors.xml
文件中)。
Android 不会抱怨你在哪里设置它,但它必须被定义为颜色资源。
I.e.:
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.
回答by Ramnath Suthakar
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 文件,然后执行如下操作。
e.g. 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. Do not put the same file to "drawable-hdpi or drawable-hdpi-v4 and so on"
但请确保将文件 light_gray.xml 仅放在“drawable”文件夹中。不要把同一个文件放到“drawable-hdpi 或 drawable-hdpi-v4 等等”