Android开发 ImageButton加载drawable文件问题

有时候我们在给ImageButton加载背景图片的时候,会出现下面的异常:

 1 org.xmlpull.v1.XmlPullParserException: Binary XML file line #3: <item> tag requires a 'drawable' attribute or child tag defining a drawable

 2     at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:178)

 3     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:867)

 4     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:804)

 5     at com.android.layoutlib.bridge.impl.ResourceHelper.getDrawable(ResourceHelper.java:208)

 6     at android.content.res.BridgeTypedArray.getDrawable(BridgeTypedArray.java:774)

 7     at android.widget.ImageView.<init>(ImageView.java:119)

 8     at android.widget.ImageButton.<init>(ImageButton.java:85)

 9     at android.widget.ImageButton.<init>(ImageButton.java:81)

10     at sun.reflect.GeneratedConstructorAccessor23.newInstance(Unknown Source)

11     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)

12     at java.lang.reflect.Constructor.newInstance(Unknown Source)

13     at android.view.LayoutInflater.createView(LayoutInflater.java:586)

14     at android.view.BridgeInflater.onCreateView(BridgeInflater.java:86)

15     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)

16     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)

17     at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:131)

18     at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:739)

19     at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)

20     at android.view.LayoutInflater.rInflate(LayoutInflater.java:711)

21     at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:742)

22     at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)

23     at android.view.LayoutInflater.rInflate(LayoutInflater.java:711)

24     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)

25     at android.view.LayoutInflater.inflate(LayoutInflater.java:372)

26     ......

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

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

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

 

你可能感兴趣的:(imagebutton)