inflate异常:协调器布局出错

时间:2018-02-07 14:27:14

标签: android android-layout android-design-library coordinator-layout

我试图在我的寻呼机适配器中扩充布局时遇到了InflateException。 相同的项目在回收站视图中运行良好,但在调用inflate方法时崩溃。

有人有答案吗?

以下是我的代码XML R.layout.item_winner

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:background="@drawable/bg_winner_cell"
        android:layout_marginStart="@dimen/dimen_10dp"
        android:paddingStart="@dimen/dimen_14dp"
        android:paddingTop="@dimen/dimen_12dp"
        android:paddingBottom="@dimen/dimen_12dp"
        android:paddingEnd="@dimen/dimen_10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    <android.support.design.widget.CoordinatorLayout
            android:id="@+id/winner_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        <ImageView
                android:id="@+id/iv_winner"
                android:src="@drawable/ic_user_placeholder"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        <ImageView
                android:src="@drawable/ic_coordinated_star"
                app:layout_anchor="@id/iv_winner"
                app:layout_anchorGravity="end"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

    </android.support.design.widget.CoordinatorLayout>
</RelativeLayout>

这是膨胀:

View view = LayoutInflater.from(mContext).inflate(R.layout.item_winner, container, false);

Ps:包含设计库:

implementation 'com.android.support:design:26.1.0'

日志:

android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class android.support.design.widget.CoordinatorLayout
                                                               Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class android.support.design.widget.CoordinatorLayout
                                                               Caused by: java.lang.reflect.InvocationTargetException

1 个答案:

答案 0 :(得分:0)

尝试使用app:srcCompat而不是android:src

        <ImageView
            android:id="@+id/iv_winner"
            app:srcCompat="@drawable/ic_user_placeholder"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

不要忘记在build.gradle文件中添加这些行

android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}


dependencies {
 compile 'com.android.support:appcompat-v7:LATEST VERSION'
}