Android 中带有自定义 RecyclerView 的 InflateException

InflateException with Custom RecyclerView in Android

对于我的一个项目,我想构建一个自定义的 recyclerview。所以,我通过以下方式扩展了 RecyclerView:

class MyCustomRecyclerView : RecyclerView {

    constructor(context: Context) : super(context){
        initializationCode(context)
    }
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs){
        initializationCode(context)
    }
    ...
}

为了简洁起见,我排除了自定义 RecyclerView 的大部分内容。如您所见,我还在 Kotlin 中添加了所需的构造函数作为辅助构造函数。 特别是带有 AttributeSet 的构造函数,这对于此类自定义视图实现至关重要。现在,膨胀后的 XML 看起来像这样:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="postListViewModel"
            type="com.celik.abdullah.myproject.viewmodels.PostFragmentViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.fragments.PostFragment">

        <com.celik.abdullah.myproject.util.MyCustomRecyclerView
            android:id="@+id/custom_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:padding="6dp"
            android:clipToPadding="false"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:postListData="@{postListViewModel.postList}"
            tools:listitem="@layout/post_list_item">

        </com.celik.abdullah.myproject.util.MyCustomRecyclerView>

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

但我得到以下异常:

Process: com.celik.abdullah.myproject, PID: 4282
    android.view.InflateException: Binary XML file line #17: Binary XML file line #17: Error inflating class com.celik.abdullah.myproject.util.MyCustomRecyclerView
    Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class com.celik.abdullah.myproject.util.MyCustomRecyclerView
    Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
        at android.view.LayoutInflater.createView(LayoutInflater.java:647)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at androidx.databinding.DataBindingUtil.inflate(DataBindingUtil.java:126)
        at androidx.databinding.DataBindingUtil.inflate(DataBindingUtil.java:95)
        at com.celik.abdullah.myproject.ui.fragments.PostFragment.onCreateView(PostFragment.kt:35)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
        at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManagerImpl.java:2100)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1874)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1830)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
        at androidx.fragment.app.FragmentManagerImpl.run(FragmentManagerImpl.java:150)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

有人可以帮忙吗?

您正在使用 layoutManager 属性 并且正在设置 androidxLinearLayoutManager

app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"

尝试从 XML 中删除它并从 Kotlin 文件动态设置它

请仔细检查您是否从 androidxRecyclerView 扩展 MyCustomRecyclerView 中的 RecyclerView

因为,RecyclerViewandroidx 之外也有

请检查androidX and withoutAndroidX版本以供参考

看起来您的初始化代码在 inflation 期间抛出异常。 InvocationTargetException 包装了构造函数抛出的异常。