错误膨胀 class de.hdodenhof.circleimageview.CircleImageView

Error inflating class de.hdodenhof.circleimageview.CircleImageView

不知道为什么它不起作用。给出的答案 不适用于我,因为我的尺寸已经在 dp.

这是我的 activity

hits_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:algolia="http://schemas.android.com/apk/res-auto">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<de.hdodenhof.circleimageview.CircleImageView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/user_image"
    android:layout_width="100dp"
    android:layout_height="100dp"
    app:civ_border_width="4dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="20dp"
    app:civ_border_color="#c42f92"
    android:scaleType="fitCenter"
    algolia:attribute='@{"image"}'/>
<TextView
    android:id="@+id/user_name"
    android:paddingTop="20dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    algolia:attribute='@{"username"}'
    algolia:highlighted='@{true}'/>
</LinearLayout>
</layout>

这是因为您错误地将标签添加为视图 xml。这是错误的:

<!-- Below line is in a wrong place and wrong tag. -->
<layout xmlns:algolia="http://schemas.android.com/apk/res-auto">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

   ...
</LinearLayout>

应该是:

<LinearLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:algolia="http://schemas.android.com/apk/res-auto"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:orientation="horizontal"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

   ...

</LinearLayout>

这一行会让你的应用崩溃

android:scaleType="fitCenter"

来自 CircleImageView github.

Limitations

The ScaleType is always CENTER_CROP and you'll get an exception if you try to change it. This is (currently) by design as it's perfectly fine for profile images.

在 logcat 你会看到这个错误。

Caused by: java.lang.IllegalArgumentException: ScaleType FIT_CENTER not supported.

解决方案:从布局xml文件中删除android:scaleType="fitCenter"