自定义分离器引起例外

发布于 2025-02-14 00:11:41 字数 2886 浏览 1 评论 0 原文

我有自定义的视图分离器,由于某种原因,其背景颜色选择器正在引起异常。如果我更改背景颜色以静态价值的功能完美。

这个问题的原因是什么?

 <include
   layout="@layout/field_underline"
   android:id="@+id/input_underline"
   android:layout_width="wrap_content"
   android:layout_height="1dp"
   android:layout_below="@+id/main_field"/>

field_underline.xml

<View xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/field_underline_color" />

field_underline_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:exitFadeDuration="200"
    android:enterFadeDuration="200">
    <item
        android:state_enabled="false"
        android:state_checked="false"
        android:state_activated="false"
        android:alpha="0.4"
        android:color="@color/content_50"
        />
    <item
        android:state_enabled="true"
        android:state_checked="true"
        android:state_activated="false"
        android:alpha="1"
        android:color="@color/invalid"
        />
    <item
        android:state_enabled="true"
        android:state_checked="false"
        android:state_activated="true"
        android:alpha="1"
        android:color="@color/primary"
        />
    <item
        android:alpha="1"
        android:color="@color/content_50"/>
</selector>

异常:

android.view.InflateException: Binary XML file line #71: Binary XML file line #4: Error inflating class <unknown>
    Caused by: android.view.InflateException: Binary XML file line #4: Error inflating class <unknown>
    Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at android.view.LayoutInflater.createView(LayoutInflater.java:647)
        at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:720)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:965)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:859)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at androidx.databinding.DataBindingUtil.inflate(DataBindingUtil.java:126)
        at androidx.databinding.DataBindingUtil.inflate(DataBindingUtil.java:95)

I have custom View separator and for some reason its background color selector is causing Exception. If I change background color to static value its working perfectly.

What is the cause of this issue?

 <include
   layout="@layout/field_underline"
   android:id="@+id/input_underline"
   android:layout_width="wrap_content"
   android:layout_height="1dp"
   android:layout_below="@+id/main_field"/>

field_underline.xml

<View xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/field_underline_color" />

field_underline_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:exitFadeDuration="200"
    android:enterFadeDuration="200">
    <item
        android:state_enabled="false"
        android:state_checked="false"
        android:state_activated="false"
        android:alpha="0.4"
        android:color="@color/content_50"
        />
    <item
        android:state_enabled="true"
        android:state_checked="true"
        android:state_activated="false"
        android:alpha="1"
        android:color="@color/invalid"
        />
    <item
        android:state_enabled="true"
        android:state_checked="false"
        android:state_activated="true"
        android:alpha="1"
        android:color="@color/primary"
        />
    <item
        android:alpha="1"
        android:color="@color/content_50"/>
</selector>

Exception:

android.view.InflateException: Binary XML file line #71: Binary XML file line #4: Error inflating class <unknown>
    Caused by: android.view.InflateException: Binary XML file line #4: Error inflating class <unknown>
    Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at android.view.LayoutInflater.createView(LayoutInflater.java:647)
        at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:720)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:965)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:859)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at androidx.databinding.DataBindingUtil.inflate(DataBindingUtil.java:126)
        at androidx.databinding.DataBindingUtil.inflate(DataBindingUtil.java:95)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

未蓝澄海的烟 2025-02-21 00:11:41

分离器需要这些行动吗?似乎并非如此。

Does the separator need those actions? It doesn't seem so.

眉黛浅 2025-02-21 00:11:41

您需要在logcat中向下看较低,才能找到真正的错误。我相信,如果您向下滚动,您会看到类似于以下错误的内容:

e/androidruntime:由:org.xmlpull.v1.xmlpullparserexception:binary xml文件行#6:标签需要“可绘制的”属性或儿童标签,以定义可绘制的

如果您查看 statelistdrawable>

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:constantSize=["true" | "false"]
    android:dither=["true" | "false"]
    android:variablePadding=["true" | "false"] >
    <item
        android:drawable="@[package:]drawable/drawable_resource"
        android:state_pressed=["true" | "false"]
        android:state_focused=["true" | "false"]
        android:state_hovered=["true" | "false"]
        android:state_selected=["true" | "false"]
        android:state_checkable=["true" | "false"]
        android:state_checked=["true" | "false"]
        android:state_enabled=["true" | "false"]
        android:state_activated=["true" | "false"]
        android:state_window_focused=["true" | "false"] />
</selector>

则标签需要一个“可绘制”属性或儿童标记,以定义可绘制的drawable。 href =“ https://developer.android.com/guide/topics/resources/drawable-resource#statelist ”

Android:可绘制

&nbsp;&nbsp;&nbsp; nbsp; drable Resource。 必需。引用可绘制的资源。

您缺少可绘制的属性。

You need to look lower down in the logcat to find the true error. I believe that if you scroll down, you will see something similar to the following error:

E/AndroidRuntime: Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #6: tag requires a 'drawable' attribute or child tag defining a drawable

Now, if you look at the documentation for a StateListDrawable, you will see the following:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:constantSize=["true" | "false"]
    android:dither=["true" | "false"]
    android:variablePadding=["true" | "false"] >
    <item
        android:drawable="@[package:]drawable/drawable_resource"
        android:state_pressed=["true" | "false"]
        android:state_focused=["true" | "false"]
        android:state_hovered=["true" | "false"]
        android:state_selected=["true" | "false"]
        android:state_checkable=["true" | "false"]
        android:state_checked=["true" | "false"]
        android:state_enabled=["true" | "false"]
        android:state_activated=["true" | "false"]
        android:state_window_focused=["true" | "false"] />
</selector>

According to this same documentation:

android:drawable

    Drawable resource. Required. Reference to a drawable resource.

You are missing the drawable attribute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文