【问题标题】:InflateException with Custom RecyclerView in AndroidAndroid 中带有自定义 RecyclerView 的 InflateException
【发布时间】:2020-01-01 11:12:41
【问题描述】:

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

有人可以帮忙吗?

【问题讨论】:

  • 您能否提供更多MyCustomRecyclerView 的详细信息?在这一点上,我觉得没问题
  • 看起来您的初始化代码在通货膨胀期间抛出异常。 InvocationTargetException 包装了构造函数抛出的异常。
  • 请发帖PostFragmentViewModel
  • @Bracadabra:谢谢。实际上,我有一个 NullPointerException ,其中的变量具有可为空的类型。我解决了那个。现在,它可以工作了。
  • 酷,然后我将复制一条评论,不要让问题悬而未决。

标签: android android-recyclerview android-custom-view inflate-exception


【解决方案1】:

看起来您的初始化代码在通货膨胀期间引发了异常。 InvocationTargetException 封装了构造函数抛出的异常。

【讨论】:

  • 再次感谢您。阅读您的答案后,我更仔细地查看了堆栈跟踪,还发现首先抛出了 NullPointerException,然后抛出了 InflateException。解决 NullPointerException 后,不再抛出 InflateException。
  • 我有同样的问题,请用我能理解的简单方式解释一下,谢谢,
【解决方案2】:

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

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

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

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

因为,RecyclerView 也可以在 androidx 之外使用

请查看androidXwithoutAndroidX 版本以供参考

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 2016-07-31
    • 2022-11-02
    • 2021-01-03
    • 1970-01-01
    相关资源
    最近更新 更多