Android开发 ImageButton加载drawable文件问题

有时候我们在给ImageButton加载背景图片的时候,会出现下面的异常:
org.xmlpull.v1.XmlPullParserException: Binary XML file line #3: <item> tag requires a 'drawable' attribute or child tag defining a drawable
	at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:178)
	at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:867)
	at android.graphics.drawable.Drawable.createFromXml(Drawable.java:804)
	at com.android.layoutlib.bridge.impl.ResourceHelper.getDrawable(ResourceHelper.java:208)
	at android.content.res.BridgeTypedArray.getDrawable(BridgeTypedArray.java:774)
	at android.widget.ImageView.<init>(ImageView.java:119)
	at android.widget.ImageButton.<init>(ImageButton.java:85)
	at android.widget.ImageButton.<init>(ImageButton.java:81)
	at sun.reflect.GeneratedConstructorAccessor23.newInstance(Unknown Source)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
	at java.lang.reflect.Constructor.newInstance(Unknown Source)
	at android.view.LayoutInflater.createView(LayoutInflater.java:586)
	at android.view.BridgeInflater.onCreateView(BridgeInflater.java:86)
	at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
	at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
	at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:131)
	at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:739)
	at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
	at android.view.LayoutInflater.rInflate(LayoutInflater.java:711)
	at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:742)
	at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
	at android.view.LayoutInflater.rInflate(LayoutInflater.java:711)
	at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
	at android.view.LayoutInflater.inflate(LayoutInflater.java:372)
	......

之所以会出现上面的异常,一般是在我们想给ImageButton动态加载图片,而用xml文件来设置切换按钮的背景。但是这种异常不影响程序运行,碰到这种异常有两个解决方法:

1.直接重启Eclipse,再打开布局文件就不会出项异常了。

2.如果你用的是纯颜色的背景,不是图片,那你不能直接在xml文件直接填写颜色值,需要values文件夹中color.xml(或是drawable.xml)设置颜色值,然后使用即可。

你可能感兴趣的:(Android开发 ImageButton加载drawable文件问题)