简体   繁体   English

膨胀类时出错<unknown>

[英]Error inflating class <unknown>

I am trying to work my way in UI. 我正试图在UI中工作。 I am trying to set stateListDrawable for list entries. 我正在尝试为列表条目设置stateListDrawable。 All I am trying to do is change the color of the list item's layout when the item is pressed, and while the list item is pressed I want to change the color of the text as well. 我想要做的就是在按下项目时更改列表项目布局的颜色,并且在按下列表项目时我也想要更改文本的颜色。

I am getting the following error stack: 我收到以下错误堆栈:

E/AndroidRuntime(  360): FATAL EXCEPTION: main
E/AndroidRuntime(  360): android.view.InflateException: Binary XML file line #8: Error inflating class <unknown>
E/AndroidRuntime(  360):    at android.view.LayoutInflater.createView(LayoutInflater.java:513)
E/AndroidRuntime(  360):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
E/AndroidRuntime(  360):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
E/AndroidRuntime(  360):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
E/AndroidRuntime(  360):    at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
E/AndroidRuntime(  360):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
E/AndroidRuntime(  360):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)

The XML that is inflated is the following: 膨胀的XML如下:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/help_list_container"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5dip"
    android:background="@drawable/default_list_selection">
    <TextView android:id="@+id/help_list_text" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textColor="@drawable/help_text_color">
    </TextView>
</LinearLayout>

I can get the program to work if I remove the android:textColor property from the xml. 如果我从xml中删除android:textColor属性,我可以让程序工作。 Is there a way I can use a stateListDrawable to control the texColor of a listitem from the xml? 有没有办法可以使用stateListDrawable来控制xml中的listitem的texColor?

The stateListDrawable works for android:background in LinearLayout, but it won't for the textColor property of TextView. stateListDrawable适用于LinearLayout中的android:background ,但不适用于TextView的textColor属性。 The state list xml is the following: 状态列表xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/white"
              android:state_pressed="true" />
    <item android:drawable="@color/black" />
</selector>

Any response would be appreciated. 任何回应将不胜感激。

I was wrongly using a drawable to change the textColor of a TextView. 我错误地使用drawable来更改TextView的textColor。 Instead, I should have used a ColorStateList 相反,我应该使用ColorStateList

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_pressed="true" android:color="@color/white" />
   <item android:color="@color/black"/>
 </selector>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM