Android中带有自定义RecyclerView的InflateException
对于我的一个项目,我想构建一个定制的recyclerview。因此,我通过以下方式扩展了RecyclerView:Android中带有自定义RecyclerView的InflateException,android,android-recyclerview,android-custom-view,inflate-exception,Android,Android Recyclerview,Android Custom View,Inflate Exception,对于我的一个项目,我想构建一个定制的recyclerview。因此,我通过以下方式扩展了RecyclerView: class MyCustomRecyclerView : RecyclerView { constructor(context: Context) : super(context){ initializationCode(context) } constructor(context: Context, attrs: AttributeSet
class MyCustomRecyclerView : RecyclerView {
constructor(context: Context) : super(context){
initializationCode(context)
}
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs){
initializationCode(context)
}
...
}
为了简洁起见,我排除了CustomRecyclerView的大部分内容。如您所见,我还在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>
有人能帮忙吗?您正在使用
layoutManager
属性,并且正在设置androidx
的LinearLayoutManager
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
尝试从XML中删除它,并从Kotlin文件中动态设置它
请仔细检查您是否在MyCustomRecyclerView
中从androidx
的RecyclerView
扩展RecyclerView
同样,RecyclerView
也可在androidx
之外使用
请检查版本以供参考您正在使用
layoutManager
属性,并且正在设置androidx
的LinearLayoutManager
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
尝试从XML中删除它,并从Kotlin文件中动态设置它
请仔细检查您是否在MyCustomRecyclerView
中从androidx
的RecyclerView
扩展RecyclerView
同样,RecyclerView
也可在androidx
之外使用
请检查版本以供参考您的初始化代码在膨胀过程中可能引发异常。InvocationTargetException包装构造函数引发的异常。看起来初始化代码在膨胀期间引发异常。InvocationTargetException包装构造函数引发的异常。您能否包含有关
MyCustomRecyclerView
的更多详细信息?在这一点上,我觉得还可以,因为您的初始化代码在膨胀期间抛出异常。InvocationTargetException包装构造函数引发的异常。请发布PostFragmentViewModel
class@Bracadabra:谢谢。事实上,我有一个NullPointerException,其中的一个变量具有可为Null的类型。我解决了这个问题。现在,它起作用了。很酷,那么我将复制一条评论,不让问题悬而未决。你能提供更多关于mycustomrecyclererview
的详细信息吗?在这一点上,我觉得还可以,因为您的初始化代码在膨胀期间抛出异常。InvocationTargetException包装构造函数引发的异常。请发布PostFragmentViewModel
class@Bracadabra:谢谢。事实上,我有一个NullPointerException,其中的一个变量具有可为Null的类型。我解决了这个问题。现在,它起作用了。很酷,那么我将复制一条评论,不要让问题悬而未决。再次感谢。在阅读了您的答案后,我更仔细地查看了stacktrace,还发现首先抛出了NullPointerException,然后抛出了InflateException。在解决了NullPointerException之后,InflateException不再抛出。我有同样的问题,请用我能理解的简单方式解释。谢谢,再次感谢。在阅读了您的答案后,我更仔细地查看了stacktrace,还发现首先抛出了NullPointerException,然后抛出了InflateException。在解决了NullPointerException之后,InflateException不再抛出。我有同样的问题,请以我能理解的简单方式解释,谢谢,