InflateException,带有Android中的Custom RecyclerView

问题描述 投票:1回答:2

对于我的一个项目,我想构建一个自定义的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$2.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)

有人可以帮忙吗?

android android-recyclerview android-custom-view inflate-exception
2个回答
0
投票

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

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

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

仔细检查是否从RecyclerViewMyCustomRecyclerView扩展androidx中的RecyclerView。>

同样,RecyclerViewandroidx之外也可用

请检查androidXwithoutAndroidX版本以供参考


0
投票

看来您的initializeCode在通货膨胀期间引发了异常。 InvocationTargetException包装构造函数引发的异常。

© www.soinside.com 2019 - 2024. All rights reserved.