Search code examples
androidlayout-inflaterandroid-inflate

Android view inflate error


I want inflate a view from a xml, just like this :

View textView = LayoutInflater.from(context).inflate(R.layout.item_classify_left, null);

But I got a error on the code:

android.view.InflateException: Binary XML file line #2: Error inflating class TextView
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:472)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:400)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:353)

There is the layout:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="40dip"
    android:gravity="center"
    android:textColor="@drawable/item_classify_left_textcolor_selector"
    android:background="@drawable/item_classify_left_background_selector" />

I found when I delete the android:textColor and android:background, It work ok. But I really need the selector. There are the selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="@color/main_background_color" />
    <item android:color="@color/item_classify_text_background_normal" />
</selector>

How can I solve this question ?


Solution

  • put the item_classify_left_textcolor_selector.xml to folder res/color ,and use @color/item_classify_left_textcolor_selector

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="40dip"
        android:gravity="center"
        android:textColor="@color/item_classify_left_textcolor_selector"
        android:background="@drawable/item_classify_left_background_selector" />