3
我在可繪製文件夾上有一系列圖像,我想在語言文件夾(drawable-p)中重複使用,因此我爲語言文件夾中的每個圖像創建了一個xml文件,如下所示:調用XML可繪製的XML按鈕
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/habitat_over" />
然後我有一個XML文件夾,表示按鈕狀態的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/habitat_over"/>
<item android:drawable="@drawable/habitat" />
</selector>
佈局
我有這樣的事情:
<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>
它應該工作,但是當我運行應用程序這個錯誤顯示出來:
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): ... 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
有誰知道爲什麼出現這種情況?
在此先感謝!
你能否解釋一些關於爲什麼以及如何做這些?並將你的完整xml文件和源代碼放在你使用這些佈局的地方 – Marmoy