-2

尝试在寻呼机适配器中膨胀布局时出现 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
4

1 回答 1

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'
}
于 2018-02-07T14:49:12.310 回答