Error : <item> tag requires a 'drawable' attribute or child tag defining a drawable
Asked Answered
C

12

55

I created a state-list selector file item_bg_selector.xml in the drawable folder like this :

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_selected="true" 
      android:drawable="@drawable/item_background_selected" />
  <item android:state_focused="true" 
      android:drawable="@drawable/item_background_selected" />
  <item android:state_pressed="true" 
      android:drawable="@drawable/item_background_selected" />
  <item android:drawable="@android:color/transparent"/>
</selector>

Then i define a style element

<style name="Item">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:background">@drawable/item_bg_selector</item>
</style>

Then I try to create an ImageButton like this :

    <ImageButton style="@style/Item"
        android:id="@id/btn_home"
    android:src="@drawable/home_button" />

Eclipse keeps giving me the following error in the Event Details window :

Failed to parse file /path/to/projectdir/res/drawable/item_bg_selector.xml

The exception trace is given below :

org.xmlpull.v1.XmlPullParserException: Binary XML file line #16: <item> tag requires a 'drawable' attribute or child tag defining a drawable
    at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:167)
    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
    at com.android.layoutlib.bridge.impl.ResourceHelper.getDrawable(ResourceHelper.java:208)
    at com.android.layoutlib.bridge.android.BridgeTypedArray.getDrawable(BridgeTypedArray.java:731)
    at android.view.View.<init>(View.java:1885)
    at android.widget.ImageView.<init>(ImageView.java:112)
    at android.widget.ImageButton.<init>(ImageButton.java:85)
    at android.widget.ImageButton.<init>(ImageButton.java:81)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at android.view.LayoutInflater.createView(LayoutInflater.java:500)
    at com.android.layoutlib.bridge.android.BridgeInflater.onCreateView(BridgeInflater.java:84)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
    at com.android.layoutlib.bridge.android.BridgeInflater.createViewFromTag(BridgeInflater.java:129)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:296)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:279)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:318)
    at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:325)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.RenderService.createRenderSession(RenderService.java:372)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.renderWithBridge(GraphicalEditorPart.java:1317)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.recomputeLayout(GraphicalEditorPart.java:1071)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.activated(GraphicalEditorPart.java:897)
    at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditor.pageChange(LayoutEditor.java:384)
    at org.eclipse.ui.part.MultiPageEditorPart$2.widgetSelected(MultiPageEditorPart.java:290)
    at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:234)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Display.sendEvent(Display.java:3783)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1375)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1398)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1383)
    at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:1195)
    at org.eclipse.swt.custom.CTabFolder.setSelection(CTabFolder.java:2743)
    at org.eclipse.swt.custom.CTabFolder.onMouse(CTabFolder.java:1429)
    at org.eclipse.swt.custom.CTabFolder$1.handleEvent(CTabFolder.java:257)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Display.sendEvent(Display.java:3783)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1375)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1398)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1383)
    at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:1195)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3629)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3284)
    at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2640)
    at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604)
    at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2438)
    at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:671)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:664)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
    at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:115)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:369)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:620)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:575)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1408)

Now, The question is where is the error? the drawables exist in the folder. tag has a drawable attribute. I have cross checked this close to 15 times and still I have not been able to find the problem. I believe it is a very simple problem(if any). Any work around to get over with this is also appreciated.

Addition : Also, line 16 in item_bg_selector.xml corresponds to the third line from the top in the file I have posted. that is

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_selected="true" 
line16 ----> android:drawable="@drawable/item_background_selected" />
Conyers answered 13/11, 2011 at 20:4 Comment(1)
C
14

Finally, I got to know that its a problem with just ADT and it actually runs fine on the device. This corresponds to issue 21046.

Conyers answered 14/11, 2011 at 3:7 Comment(1)
It seems to have been fixed in r17; there's a preview available on tools.android.com.Bhatt
O
49

The error has to do with the format of your xml, you are setting something like

android:background="@drawable/item_bg_selector"

What the error is telling you is that it should be like this

android:drawable="@drawable/item_bg_selector"
Olivo answered 24/11, 2011 at 19:5 Comment(3)
I tried so many things, switched to intellij even, when nothing worked. This solution saved my day. Thank you so much.Lamasery
I tried changing "android:background" to "android:drawable". It doesn't work.Heckelphone
But this not solve the purpose , crash will remove but background is not settingFlatt
I
46

I've experienced the same Exception for a different problem.

I was trying to use a color selector for the View's android:background:

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/tab_background_selector" />

<!-- /res/color/tab_background_selector.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/white" android:state_checked="true" />
    <item android:color="@color/light_blue" />
</selector>

Moving the selector to the drawable folder and changing android:color to android:drawable solved the problem.

Also, the background definition in the Layout needs to be changed to android:background="@drawable/tab_background_selector"

Here is the final selector:

<!-- /res/drawable/tab_background_selector.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/white" android:state_checked="true" />
    <item android:drawable="@color/light_blue" />
</selector>
Indraft answered 21/12, 2017 at 13:37 Comment(4)
Also, cannot use directly RGB color code such as #121212 in android:drawable. We have to use color resource xml file.Keever
Is there a way to catch this issue pre-release?Fissi
@CodeWiget I would say make sure you test all defined states of your components (focused/checked/selected etc.) as this is a runtime issue.Indraft
This is the correct complete answer!Bumf
F
20

I had the same error and switching the order of the attributes (as it was suggested in the this answer to this other question) to have the drawable the first one solved the problem.

In your case would be switching:

  <item android:state_selected="true" 
   android:drawable="@drawable/item_background_selected" />

to:

  <item android:drawable="@drawable/item_background_selected"
   android:state_selected="true" />
Freehanded answered 24/10, 2012 at 11:2 Comment(3)
Your solution worked! But I wonder why like that. We are defining the drawable for the state. Not the other way round:)Colossal
Even the official docs are wrong about thisMilliner
https://mcmap.net/q/125603/-android-selector-amp-text-colorBendy
C
14

Finally, I got to know that its a problem with just ADT and it actually runs fine on the device. This corresponds to issue 21046.

Conyers answered 14/11, 2011 at 3:7 Comment(1)
It seems to have been fixed in r17; there's a preview available on tools.android.com.Bhatt
I
7

similar to this answer, I need to make a drawable version of the color resource.

solution

  1. instead of modifying the existing color state list file, craete a new drawable file

    new_drawable_file.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="@color/old_color_state_list" />
    </shape>
    
  2. then i updated the problematic usages of @color/old_color_state_list to @drawable/new_drawable_file.

background

  • in my styles.xml, i had overridden ?android:activatedBackgroundIndicator with my @color/old_color_state_list
    <item name="android:activatedBackgroundIndicator">@color/old_color_state_list</item>
    
  • but the documentation for ?android:activatedBackgroundIndicator says it should be a drawable resource, not a color:

    Drawable used as a background for activated items.

  • so, i updated my styles.xml to override ?android:activatedBackgroundIndicator with my @drawable/new_drawable_file drawable, instead.
    <item name="android:activatedBackgroundIndicator">@drawable/new_drawable_file</item>
    
Idomeneus answered 26/11, 2020 at 18:56 Comment(0)
A
1

I also encountered the same issue.

I just restarted eclipse with -clean, and the did a project -> Clean on the project in question and the errors have gone away.

Actinouranium answered 22/11, 2011 at 19:13 Comment(1)
may be it depends on the os we use also. It dint work on Mac.Conyers
R
1

Restarting Eclipse and cleaning the project worked for me.

Reinhart answered 8/2, 2012 at 0:38 Comment(0)
E
1

The same error occurs on the activity_main.xml file if there is an extra LinearLayout in the Activity. Removing this solves the issue.

Cleaning and other things do not help.

Escalate answered 17/11, 2016 at 20:1 Comment(0)
P
1

The drawable inside the file (a png) was corrupted. I just saved again the file and it worked.

Protrusion answered 12/10, 2018 at 15:6 Comment(0)
T
0

I got it to work by copying the image right into the file system (Finder on Mac) rather than through Android Studio.

Tonneau answered 9/5, 2014 at 15:32 Comment(0)
L
0

I had the problem because when renaming the selector for pre-lollipop devices, I forgot to rename the ripple with th e same name also in the drawable-v21 folder.

Lucifer answered 26/7, 2015 at 17:41 Comment(0)
J
0
         In my case I was applying color attribute to checkedIcon attribute, Instead 
         of chipIconTint, so replacing checkedIcon with chipIconTint in style solved 
         my problem.
           Use   
          <item name="chipIconTint">@color/chip_text_color</item>
           Instead of
          <item name="checkedIcon">@color/chip_text_color</item>
Jestinejesting answered 24/10, 2021 at 11:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.