开发者

XML buttons that call XML drawables

I have a series of images on drawable folder that I want to reuse in a language folder (drawable-pt) so I created an xml file for each image in the language folder like this:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/habitat_over" />

then I have in a xml folder a xml file that represents button states:

<?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/habitat_over"/>
    <item android:drawable="@drawable/habitat" />
</selector>

in the layout I have something like this:

<ImageButton android:layout_weight="1" 
android:background="@null" 
android:layout_width="wrap_content" 
android:src="@xml/habitat_button" 
android:id="@+id/habitat" 
android:layout_height="wrap_content" 
android:scaleType="fitCenter">
</ImageButton>

It should work but when I run the application this error shows up:

Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class <unknown>

E/AndroidRuntime(  409):    at android.view.LayoutInflater.createView(LayoutInflater.java:518)

E/AndroidRuntime(  409):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)

E/AndroidRuntime(  409):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)

E/AndroidRuntime(  409):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)

E/AndroidRuntime(  409):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)

E/AndroidRuntime(  409):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)

E/AndroidRuntime(  409):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)

E/AndroidRuntime(  409):    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)

E/AndroidRuntime(  409):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)

E/AndroidRuntime(  409):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)

E/AndroidRuntime(  409):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)

E/AndroidRuntime(  409):    at android.app.Activity.setContentView(Activity.java:1657)

E/AndroidRuntime(  409):    at pnm.freiramadeira.ChapterSelection.onCreate(ChapterSelection.java:90)

E/AndroidRuntime(  409):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

E/AndroidRuntime(  409):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

E/AndroidRuntime(  409开发者_C百科):    ... 11 more

E/AndroidRuntime(  409): Caused by: java.lang.reflect.InvocationTargetException

E/AndroidRuntime(  409):    at java.lang.reflect.Constructor.constructNative(Native Method)

E/AndroidRuntime(  409):    at java.lang.reflect.Constructor.newInstance(Constructor.java:415)

E/AndroidRuntime(  409):    at android.view.LayoutInflater.createView(LayoutInflater.java:505)

E/AndroidRuntime(  409):    ... 25 more

E/AndroidRuntime(  409): Caused by: android.content.res.Resources$NotFoundException: File res/xml/habitat_button.xml from drawable resource ID #0x7f050007

E/AndroidRuntime(  409):    at android.content.res.Resources.loadDrawable(Resources.java:1697)

E/AndroidRuntime(  409):    at android.content.res.TypedArray.getDrawable(TypedArray.java:601)

E/AndroidRuntime(  409):    at android.widget.ImageView.<init>(ImageView.java:118)

E/AndroidRuntime(  409):    at android.widget.ImageButton.<init>(ImageButton.java:85)

E/AndroidRuntime(  409):    at android.widget.ImageButton.<init>(ImageButton.java:81)

E/AndroidRuntime(  409):    ... 28 more

E/AndroidRuntime(  409): Caused by: android.content.res.Resources$NotFoundException: File res/drawable-pt/habitat_over.xml from drawable resource ID #0x7f02002f

E/AndroidRuntime(  409):    at android.content.res.Resources.loadDrawable(Resources.java:1697)

E/AndroidRuntime(  409):    at android.content.res.Resources.getDrawable(Resources.java:581)

E/AndroidRuntime(  409):    at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)

E/AndroidRuntime(  409):    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)

E/AndroidRuntime(  409):    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)

E/AndroidRuntime(  409):    at android.content.res.Resources.loadDrawable(Resources.java:1694)

E/AndroidRuntime(  409):    ... 32 more

E/AndroidRuntime(  409): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #2: <bitmap> requires a valid src attribute

E/AndroidRuntime(  409):    at android.graphics.drawable.BitmapDrawable.inflate(BitmapDrawable.java:375)

E/AndroidRuntime(  409):    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)

E/AndroidRuntime(  409):    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)

E/AndroidRuntime(  409):    at android.content.res.Resources.loadDrawable(Resources.java:1694)

E/AndroidRuntime(  409):    ... 37 more

Does anyone knows why this happens??

Thanks in advance!!


Even though it's not really documented, the android:src attribute of a <bitmap> only accepts image files (not drawables defined in XML).

Instead of having src="@drawable/habitat_over" in a bitmap and referencing that <bitmap> from your layout, remove the <bitmap> and add a direct reference to from the layout to @drawable/habitat_over.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜