错误:<item>标记需要一个'drawable'属性或定义drawable </item>的子标记

时间:2011-11-13 20:04:56

标签: android layout imagebutton statelistdrawable

我在drawable文件夹中创建了一个状态列表选择器文件item_bg_selector.xml,如下所示:

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

然后我定义一个样式元素

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

然后我尝试像这样创建一个ImageButton

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

Eclipse在Event Details窗口中不断给出以下错误:

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

异常跟踪如下:

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)

现在,问题在于错误在哪里? drawables存在于文件夹中。 tag具有drawable属性。我已经交叉检查了近15次,但我仍然无法找到问题。我相信这是一个非常简单的问题(如果有的话)。任何解决这个问题的工作也值得赞赏。

增加: 此外,item_bg_selector.xml中的第16行对应于我发布的文件中从顶部开始的第三行。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_selected="true" 
line16 ----> android:drawable="@drawable/item_background_selected" />

11 个答案:

答案 0 :(得分:39)

错误与xml的格式有关,您设置的内容类似于

android:background="@drawable/item_bg_selector"

错误告诉你的是它应该是这样的

android:drawable="@drawable/item_bg_selector"

答案 1 :(得分:19)

我遇到了同样的错误,并且切换了属性的顺序(正如this answer中建议的那样other question)要可绘制的第一个解决了这个问题。

在你的情况下会切换:

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

为:

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

答案 2 :(得分:12)

最后,我知道它只是ADT的一个问题,它实际上在设备上运行良好。这对应于问题21046

答案 3 :(得分:5)

我遇到了同样的异常问题。

我试图为View的{​​{1}}使用颜色选择器:

android:background

将选择器移动到drawable文件夹并将<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> 更改为android:color解决了问题。

此外,布局中的背景定义需要更改为android:drawable

这是最后的选择器:

android:background="@drawable/tab_background_selector"

答案 4 :(得分:2)

我也遇到了同样的问题。

我刚刚用-clean重新启动了eclipse,并且对该项目做了project -> Clean并且错误已经消失。

答案 5 :(得分:2)

重新启动Eclipse并清理项目对我有用。

答案 6 :(得分:1)

文件(png)中的可绘制对象已损坏。我只是再次保存了文件,它就可以了。

答案 7 :(得分:0)

我通过将图像直接复制到文件系统(Mac上的Finder)而不是通过Android Studio来实现它。

答案 8 :(得分:0)

我遇到了问题,因为在为棒棒糖前设备重命名选择器时,我忘了在drawable-v21文件夹中重命名同名的纹波。

答案 9 :(得分:0)

如果activity_main.xml中有额外的LinearLayout,则Activity文件会出现同样的错误。删除它可以解决问题。

清洁和其他事情没有帮助。

答案 10 :(得分:0)

similar to this answer,我需要制作drawable资源的color版本。

已采取的步骤:

  1. 我创建了一个新的可绘制文件,而不是修改现有的颜色状态列表文件

    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. 然后我将@color/old_color_state_list的有问题的用法更新为@drawable/new_drawable_file

就我而言:

  • 在我的styles.xml中,我用?android:activatedBackgroundIndicator覆盖了@color/old_color_state_list
    <item name="android:activatedBackgroundIndicator">@color/old_color_state_list</item>
    
  • 但是?android:activatedBackgroundIndicator的文档说它应该是drawable的资源,而不是color

    Drawable用作激活项的背景。

  • 因此,我将styles.xml更新为使用?android:activatedBackgroundIndicator可绘制对象覆盖@drawable/new_drawable_file
    <item name="android:activatedBackgroundIndicator">@drawable/new_drawable_file</item>