Android 查看膨胀错误

Android view inflate error

我想膨胀 xml 的视图,就像这样:

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

但是我在代码上遇到了错误:

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)

有布局:

<?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" />

我发现当我删除 android:textColor 和 android:background 时,它工作正常。但我真的需要选择器。有选择器:

<?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>

我该如何解决这个问题?

item_classify_left_textcolor_selector.xml放入文件夹res/color,然后使用@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" />