Resource is not a Drawable

前端 未结 5 1733
萌比男神i
萌比男神i 2021-01-16 13:23

This code causes force close exception on Emulator(API 18) and works in ASUS Nexus 7(API 21)



        
相关标签:
5条回答
  • 2021-01-16 13:56

    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" />
    
    0 讨论(0)
  • 2021-01-16 13:59

    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.

    0 讨论(0)
  • 2021-01-16 14:06

    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"

    0 讨论(0)
  • 2021-01-16 14:08

    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.

    0 讨论(0)
  • 2021-01-16 14:11

    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.
    
    0 讨论(0)
提交回复
热议问题