Home > Net >  Custom separator is causing exception
Custom separator is causing exception

Time:07-10

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)

CodePudding user response:

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

CodePudding user response:

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.

  • Related