1

This code causes force close exception on Emulator(API 18) and works in 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.

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,

<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,

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.

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.

NOTE: I have deleted and recreated the Emulator, no use. I have tested on API 19 emulator also, same issue.

Any solution will be highly appreciable.

Community
  • 1
  • 1
Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
  • 1
    Does this answer your question? [Resources$NotFoundException: Resource is not a Drawable (color or path)?](https://stackoverflow.com/questions/29095063/resourcesnotfoundexception-resource-is-not-a-drawable-color-or-path) – VahidHoseini Dec 06 '20 at 22:05
  • @VahidHoseini I already updated my answer. – Gunaseelan Dec 07 '20 at 01:28

5 Answers5

15

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.

Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
3

Also make sure you don't have the background of an imageView set to selectableItemBackgroundBorderless, otherwise older devices will have this error:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="?android:selectableItemBackgroundBorderless" />
JoachimR
  • 5,150
  • 7
  • 45
  • 50
1

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.

The solution is to put your drawable in drawable-...dpi folders too.

akhil
  • 1,649
  • 3
  • 19
  • 31
0

Your color resource must reside in an Android resource file (i.e.: in the colors.xml file).
Android would not complain where you set it, but it must be defined as a color resource.

I.e.:

<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.

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.
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
-1

I think the best way of doing this is, first create a xml file inside the "drawable" folder and do something like below.

e.g. light_gray.xml then do something like below.

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