热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Android中带有自定义RecyclerView的InflateException

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

对于我的一个项目,我想构建一个自定义的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如下所示:

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

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

android:layout_
android:layout_
tools:cOntext=".ui.fragments.PostFragment">
android:id="@+id/custom_recycler_view"
android:layout_
android:layout_
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">



但是我得到以下异常:

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)

有人可以帮忙吗?



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

,

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

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

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

仔细检查是否从RecyclerView的{​​{1}}扩展MyCustomRecyclerView中的androidx

此外,RecyclerViewRecyclerView之外也可用

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


推荐阅读
author-avatar
秋雨蒙蒙往事
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有