Using drawable resources

2019-01-09 17:30发布

I have this problem, see the trace stack:

E/AndroidRuntime(2410): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #5: <bitmap> requires a valid src attribute

My xml file looks like:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <bitmap android:src="@drawable/btn_1"/>
    </item>
</layer-list>

btn_1 is another xml file in drawable resources When i'm using an image(*.png) instead of xml drawable it's ok.

Can I use a drawable resource as src to bitmap? Just in case here is my btn_1.xml file. It doesn't work even if btn_1 file have no items.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/btn_arrow_bg_red"/>
<item>
    <bitmap
        android:gravity="center"
        android:src="@drawable/btn_arrow_white" />
</item>
</layer-list>

3条回答
迷人小祖宗
2楼-- · 2019-01-09 17:36

To avoid error: Binary XML file line #XXX: requires a valid src attribute

inside a layer-list, use:

<item android:drawable="@drawable/image" />

instead of:

<item>
  <bitmap android:src="@drawable/image"/>
</item>
查看更多
劫难
3楼-- · 2019-01-09 17:57

I got this error (<bitmap> requires a valid src attribute) when I use vector image for bitmap.

My solution is
- Use @Santiago Rivas answer
- Or user another image (not vector image)

查看更多
4楼-- · 2019-01-09 18:00

You cant have an xml drawable as source for bitmap. Because for example if it was possible, then it could mistakenly create a black-hole by calling xml to itself.

Lets suppose, you have an xml drawable A which has a bitmap whos source is drawable B. But in drawable B, it has a bitmap whos source is drawable A. This will create a circular loop which cant be resolved. That is why you need to provide an image as a source for bitmap to avoid any confusion

查看更多
登录 后发表回答