从android中的自定义ListView将文本颜色选择器设置为CheckedTextView项时,会出现异常

时间:2014-09-02 14:54:20

标签: android listview android-listview checkedtextview inflate-exception

我有一个自定义ListView,它应该有背景和文本项的颜色选择器。

我的清单中的自定义项目:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="5dp"
    android:paddingTop="5dp" >

    <ImageView
        android:id="@+id/user_image"
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/user_name"
        android:layout_alignTop="@+id/user_name"
        android:scaleType="centerInside" />

    <CheckedTextView
        android:id="@+id/user_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="2dp"
        android:paddingBottom="2dp"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@+id/user_image"
        android:background="@drawable/list_item_selector_blue"
        android:textColor="@drawable/text_color_selector"
        android:textSize="18sp" />

</RelativeLayout>

我可以通过选择器更改CheckedTextView背景,但是当我将选择器放入文本颜色(如android:textColor="@drawable/text_color_selector")时,应用程序崩溃并出现以下错误:

09-02 11:36:25.918: E/AndroidRuntime(17612): FATAL EXCEPTION: main
09-02 11:36:25.918: E/AndroidRuntime(17612): Process: a.com.a, PID: 17612
09-02 11:36:25.918: E/AndroidRuntime(17612): android.view.InflateException: Binary XML file line #17: Error inflating class android.widget.CheckedTextView
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.LayoutInflater.createView(LayoutInflater.java:626)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at adapters.UsersListAdapter.getView(UsersListAdapter.java:44)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.widget.AbsListView.obtainView(AbsListView.java:2713)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1274)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.widget.ListView.onMeasure(ListView.java:1186)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.View.measure(View.java:17354)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.View.measure(View.java:17354)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1237)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.View.measure(View.java:17354)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.View.measure(View.java:17354)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.View.measure(View.java:17354)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.View.measure(View.java:17354)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.View.measure(View.java:17354)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.View.measure(View.java:17354)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:704)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.View.measure(View.java:17354)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.View.measure(View.java:17354)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.View.measure(View.java:17354)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:382)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.View.measure(View.java:17354)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2505)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.View.measure(View.java:17354)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2176)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1315)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1512)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1199)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6387)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.Choreographer.doCallbacks(Choreographer.java:603)
09-02 11:36:25.918: E/AndroidRuntime(17612):    at android.view.Choreographer.doFrame(Choreog

我搜索了很多,发现很多人都有这样的问题,但任何提示都可以解决我的问题。

这些是我的选择者:

text_color_selector

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

list_item_selector_blue

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

1 个答案:

答案 0 :(得分:0)

textColor属性只能有颜色值。所以你不允许在选择器XML(text_color_selector.xml)中定义android:drawable。只需将drawable更改为color,这应该可以解决问题。至少它确实改变了我的问题:

SELECT DISTINCT address.address_id, address.address1, address.streetcity, state.stateabbrev, rtrim(ltrim(case when address.streetzipcode is not null and address.streetzipcode != 'NULL' then address.streetzipcode else '' end))+case when len(address.streetzipplus4)>0 then '-'+rtrim(ltrim(address.streetzipplus4)) else '' end as streetzipcode, address.homephone,
        dbo.f_addressstudent (student.address_id) as Students, 
        dbo.f_addresspeople (student.address_id) as Adults,
        case 
            when @classif_id IS NULL then 0 
            else 
            student.classif_id 
        end classif,
        classifctn
    FROM district WITH(NOLOCK) 
        JOIN dbo.building ON building.district_id = district.district_id 
        JOIN dbo.studbldg_bridge WITH(NOLOCK) ON studbldg_bridge.bldg_id=building.bldg_id
        JOIN dbo.student WITH(NOLOCK) ON student.student_id = studbldg_bridge.student_id
        JOIN classif with(nolock) on student.classif_id = classif.classif_id
        LEFT JOIN dbo.address WITH(NOLOCK) ON student.address_id = address.address_id 
        LEFT JOIN dbo.state WITH(NOLOCK) ON address.streetstate_id = state.state_id
        LEFT JOIN dbo.state AS mailstate WITH(NOLOCK) ON address.state_id = mailstate.state_id
    WHERE district.district_id = (SELECT district_id FROM dbo.building WITH(NOLOCK) WHERE bldg_id = @bldg_id)
    ORDER BY classif,Adults, Students

我认为文本颜色只能包含颜色,而drawable允许您定义更多颜色。