其他分享
首页 > 其他分享> > android-调用XML可绘制对象的XML按钮

android-调用XML可绘制对象的XML按钮

作者:互联网

我想在语言文件夹(drawable-pt)中重用可绘制文件夹上的一系列图像,因此我为语言文件夹中的每个图像创建了一个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

有谁知道为什么会这样吗?

提前致谢!!

解决方法:

即使没有真正记载,也可以将< bitmap>的android:src属性仅接受图像文件(不接受XML定义的可绘制对象).

而不是在位图中包含src =“ @ drawable / habitat_over”并引用该< bitmap>从您的布局中删除< bitmap>并将直接引用从布局添加到@ drawable / habitat_over.

标签:android-layout,android-xml,android
来源: https://codeday.me/bug/20191208/2089480.html