无法解析可绘制对象
我正在尝试将图像添加到图像按钮。 我的button.xml 看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true" android:drawable="@drawable/button_pressed" />
<item
android:state_focused="true" android:drawable="@drawable/button_focused" />
<item
android:drawable="@drawable/button_normal" />
</selector>
我的main.xml 看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageButton
android:src="@drawable/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
我的错误看起来像这样:
org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: <item> tag requires a 'drawable' attribute or child tag defining a drawable
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:151)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:749)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:695)
at com.android.layoutlib.bridge.ResourceHelper.getDrawable(ResourceHelper.java:145)
at com.android.layoutlib.bridge.BridgeTypedArray.getDrawable(BridgeTypedArray.java:662)
at android.widget.ImageView.<init>(ImageView.java:113)
at android.widget.ImageButton.<init>(ImageButton.java:51)
at android.widget.ImageButton.<init>(ImageButton.java:47)
我知道错误集中在button.xml 中的第一个“item”标签上。我尝试将 main.xml 中的 src 更改为背景,我尝试将 button.xml 中的可绘制对象更改为背景,但每次都会遇到相同的错误。
I am trying to add images to an image button.
My button.xml looks like so:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true" android:drawable="@drawable/button_pressed" />
<item
android:state_focused="true" android:drawable="@drawable/button_focused" />
<item
android:drawable="@drawable/button_normal" />
</selector>
And my main.xml looks like so:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageButton
android:src="@drawable/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
And my error looks like so:
org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: <item> tag requires a 'drawable' attribute or child tag defining a drawable
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:151)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:749)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:695)
at com.android.layoutlib.bridge.ResourceHelper.getDrawable(ResourceHelper.java:145)
at com.android.layoutlib.bridge.BridgeTypedArray.getDrawable(BridgeTypedArray.java:662)
at android.widget.ImageView.<init>(ImageView.java:113)
at android.widget.ImageButton.<init>(ImageButton.java:51)
at android.widget.ImageButton.<init>(ImageButton.java:47)
I understand that the error is focused on the first "item" tag in the button.xml. I have tried changing src to background in the main.xml, I have tried changing drawable to background in the button.xml but I get the same error every time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过使用普通按钮而不是 imageButton?
喜欢:
Have you tried using an ordinary button instead of an imageButton?
Like: