RecycleView适配器膨胀崩溃

时间:2018-08-14 11:33:37

标签: android android-layout android-recyclerview inflate-exception

我在Android应用中使用RecycleView适配器为联系人列表添加自定义项目布局,但是如果运行它会发生错误。谁能帮我吗?

我的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="2dp"
    app:cardBackgroundColor="@color/white"
    app:cardCornerRadius="4dp"
    app:cardPreventCornerOverlap="true"
    app:contentPadding="4dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="8dp">

        <RelativeLayout
            android:id="@+id/topContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/avatar"
                style="@style/ContactListAvatarBlackTextStyle"
                android:layout_width="42dp"
                android:layout_height="42dp"
                android:layout_centerVertical="true"
                android:background="@drawable/contactlist_avatar_bg_black"
                android:gravity="center"
                android:text="E"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/lbl_contactName"
                style="@style/TextStyle51"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="28dp"
                android:layout_toRightOf="@+id/avatar"
                android:text="Bechir Segni" />
        </RelativeLayout>

        <LinearLayout
            android:id="@+id/action_buttons"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/topContainer"
            android:orientation="horizontal"
            android:weightSum="2">

            <Button
                android:id="@+id/btn_modify"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/app_name"
                android:theme="@style/ContactActionButtonTheme" />

            <Button
                android:id="@+id/btn_delete"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/app_name"
                android:theme="@style/ContactActionButtonTheme" />
        </LinearLayout>


    </RelativeLayout>
</android.support.v7.widget.CardView>

我的风格是:

<style name="ContactActionButtonTheme" parent="Widget.AppCompat.Button.Colored"> 
    <item name="android:textColor">@drawable/button_text_selector</item> 
    <item name="colorButtonNormal">@drawable/contact_action_button_bg</item> 
    <item name="colorControlHighlight">@color/colorHighlight</item> 
</style>

我的日志猫:

android.view.InflateException: Binary XML file line #53: Error inflating class Button
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:770)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
    at tech.digitus.tayarapay.ui.contactlist.ContactListActivityAdapter.onCreateViewHolder(ContactListActivityAdapter.java:108)
    at tech.digitus.tayarapay.ui.contactlist.ContactListActivityAdapter.onCreateViewHolder(ContactListActivityAdapter.java:22)
    at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6685)

3 个答案:

答案 0 :(得分:2)

尽量避免使用drawable放置textColor

<style name="ContactActionButtonTheme" parent="Widget.AppCompat.Button.Colored"> 
    <item name="android:textColor">@color/textColor</item> 
    <item name="colorButtonNormal">@color/colorButtonNormal</item> 
    <item name="colorControlHighlight">@color/colorHighlight</item> 
</style>

提供您要使用的颜色。

答案 1 :(得分:0)

删除行:

android:layout_weight="1"

答案 2 :(得分:0)

非常感谢,我找到了解决方案,问题出在“ @ drawable / contact_action_button_bg”上的可绘制对象上

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <corners android:radius="4dp" />
            <stroke android:width="1dp"
                android:color="@color/colorPrimary" />
            <solid android:color="@color/colorPrimary"></solid>
        </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="rectangle">
            <corners android:radius="4dp" />
            <stroke android:width="1dp"
                android:color="@color/colorPrimary" />
            <solid android:color="@color/colorPrimary"></solid>
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="4dp" />
            <stroke android:width="1dp"
                android:color="@color/colorPrimary" />
            <solid android:color="@color/white"></solid>
        </shape>
    </item>
</selector>

我用这个替换了

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/colorButtonActived" android:state_activated="true" />
    <item android:color="@color/white" android:state_enabled="false" />
    <item android:color="@color/white" android:state_active="false" />
    <item android:color="@color/white" />
</selector>