充气 RecyclerView 时出现 InflateException

Posted

技术标签:

【中文标题】充气 RecyclerView 时出现 InflateException【英文标题】:InflateException when inflating RecyclerView 【发布时间】:2021-12-17 07:57:29 【问题描述】:

我正在开发这个应用程序,现在它在启动时崩溃。这是崩溃的日志:

2021-11-02 16:50:27.012 25115-25115/br.com.alura.boardgamestore E/androidRuntime: FATAL EXCEPTION: main
    Process: br.com.alura.boardgamestore, PID: 25115
    android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class com.google.android.material.card.MaterialCardView
    Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class com.google.android.material.card.MaterialCardView
    Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at android.view.LayoutInflater.createView(LayoutInflater.java:686)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:829)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:531)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:461)
        at br.com.alura.boardgamestore.databinding.ProductItemBinding.inflate(ProductItemBinding.java:63)
        at br.com.alura.boardgamestore.ui.recyclerview.adapter.ProductsListAdapter.onCreateViewHolder(ProductsListAdapter.kt:56)
        at br.com.alura.boardgamestore.ui.recyclerview.adapter.ProductsListAdapter.onCreateViewHolder(ProductsListAdapter.kt:15)
        at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7295)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6416)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6300)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6296)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2330)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1631)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1591)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:668)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4309)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:4012)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4578)
        at android.view.View.layout(View.java:22406)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1873)
        at android.view.View.layout(View.java:22406)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:22406)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:536)
        at android.view.View.layout(View.java:22406)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:22406)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
        at android.view.View.layout(View.java:22406)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:1079)
        at android.view.View.layout(View.java:22406)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3391)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2860)

这是包含 recyclerview 的布局的 xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_>


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/products_list_recyclerview"
        android:layout_
        android:layout_
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:listitem="@layout/product_item" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/product_list_fab"
        android:layout_
        android:layout_
        android:clickable="true"
        android:layout_margin="16dp"
        android:focusable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/ic_action_add" />


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

the xml for the item that should be displayed inside the recyclerView:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    android:layout_margin="8dp"
    app:cardCornerRadius="8dp"
    app:cardElevation="8dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_
        android:layout_>

        <TextView
            android:id="@+id/product_item_name"
            android:layout_
            android:layout_
            android:layout_margin="16dp"
            android:ellipsize="end"
            android:fontFamily="@font/montserrat_bold"
            android:maxLines="1"
            android:textSize="20sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/imageView"
            tools:text="@tools:sample/lorem/random" />

        <TextView
            android:id="@+id/product_item_description"
            android:layout_
            android:layout_
            android:ellipsize="end"
            android:fontFamily="@font/montserrat"
            android:maxLines="3"
            android:textSize="14sp"
            app:layout_constraintEnd_toEndOf="@id/product_item_name"
            app:layout_constraintStart_toStartOf="@id/product_item_name"
            app:layout_constraintTop_toBottomOf="@id/product_item_name"
            tools:text="@tools:sample/lorem/random" />

        <TextView
            android:id="@+id/product_item_price"
            android:layout_
            android:layout_
            android:layout_marginTop="8dp"
            android:layout_marginBottom="16dp"
            android:fontFamily="@font/montserrat"
            android:textColor="@color/colorPriceText"
            android:textSize="18sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@id/number_of_players"
            app:layout_constraintStart_toStartOf="@id/product_item_name"
            app:layout_constraintTop_toBottomOf="@id/product_item_description"
            tools:text="R$ 19.99" />

        <TextView
            android:id="@+id/number_of_players"
            android:layout_
            android:layout_
            app:layout_constraintStart_toEndOf="@id/product_item_price"
            app:layout_constraintTop_toBottomOf="@id/product_item_description"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:text="@string/_2"
            android:fontFamily="@font/montserrat"
            android:textStyle="bold"
            android:textSize="18sp" />

        <ImageView
            android:id="@+id/imageView"
            android:layout_
            android:layout_
            android:layout_marginTop="16dp"
            android:scaleType="centerCrop"
            app:layout_constraintBottom_toTopOf="@id/product_item_name"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintWidth_percent="0.9"
            app:srcCompat="@drawable/default_image"
            tools:srcCompat="@tools:sample/backgrounds/scenic" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>

适配器:

package br.com.alura.boardgamestore.ui.recyclerview.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import br.com.alura.boardgamestore.extensions.tryImageLoad
import br.com.alura.boardgamestore.model.Product
import br.com.alura.boardgamestore.databinding.ProductItemBinding
import java.math.BigDecimal
import java.text.NumberFormat
import java.util.*

class ProductsListAdapter(
    private val context: Context,
    products: List<Product>
) : RecyclerView.Adapter<ProductsListAdapter.ViewHolder>() 

    private val products = products.toMutableList()

    class ViewHolder(private val binding: ProductItemBinding) :
        RecyclerView.ViewHolder(binding.root) 

        fun attach(product: Product) 
            val name = binding.productItemName
            name.text = product.name
            val description = binding.productItemDescription
            description.text = product.description
            val price = binding.productItemPrice
            val formattedPrice: String =
                brCurrencyFormat(product.price)
            price.text = formattedPrice

            val visibility = if(product.image != null)
                View.VISIBLE
             else 
                View.GONE
            

            binding.imageView.visibility = visibility

            binding.imageView.tryImageLoad(product.image)
        

        private fun brCurrencyFormat(valor: BigDecimal): String 
            val formatter: NumberFormat = NumberFormat
                .getCurrencyInstance(Locale("pt", "br"))
            return formatter.format(valor)
        

    

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder 
        val inflater = LayoutInflater.from(context)
        val binding = ProductItemBinding.inflate(inflater,parent,false)
        return ViewHolder(binding)
    

    override fun onBindViewHolder(holder: ViewHolder, position: Int) 
        val produto = products[position]
        holder.attach(produto)
    

    override fun getItemCount(): Int = products.size

    fun updateList(products: List<Product>) 
        this.products.clear()
        this.products.addAll(products)
        notifyDataSetChanged()
    


最后是主要活动:

package br.com.alura.boardgamestore.ui.activity

import android.content.Intent
import android.os.Bundle
import android.view.MenuInflater
import androidx.appcompat.app.AppCompatActivity
import br.com.alura.boardgamestore.dao.ProductsDao
import br.com.alura.boardgamestore.databinding.ProductsListActivityBinding
import br.com.alura.boardgamestore.ui.recyclerview.adapter.ProductsListAdapter
import android.R
import android.view.Menu
import android.R.menu


class ProductsListActivity : AppCompatActivity() 

    private val dao = ProductsDao()
    private val adapter = ProductsListAdapter(context = this, products = dao.searchAll())
    private val binding by lazy 
        ProductsListActivityBinding.inflate(layoutInflater)
    

    

    override fun onCreate(savedInstanceState: Bundle?) 
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        setRecyclerView()
        setFab()
    

    override fun onResume() 
        super.onResume()
        adapter.updateList(dao.searchAll())
    

    private fun setFab() 
        val fab = binding.productListFab
        fab.setOnClickListener 
            goToProductForm()
        
    

    private fun goToProductForm() 
        val intent = Intent(this, ProductForm::class.java)
        startActivity(intent)
    

    private fun setRecyclerView() 
        val recyclerView = binding.productsListRecyclerview
        recyclerView.adapter = adapter
    


感谢有关如何调试和纠正此问题的任何提示。我仍然是这方面的初学者,所以我有点迷茫。我跟着一个在线课程,它工作得很好,直到它没有。谢谢!

【问题讨论】:

嗨!请添加完整的堆栈跟踪 【参考方案1】:

我认为问题出在 onCreateViewHolder 上,LayoutInflater 应该是父 ViewGroup。

之前:

val inflater = LayoutInflater.from(context)

之后:

val inflater = LayoutInflater.from(parent.context)

希望能帮到你:)

【讨论】:

以上是关于充气 RecyclerView 时出现 InflateException的主要内容,如果未能解决你的问题,请参考以下文章

实现 Recyclerview 过滤器时出现 indexoutofboundexception

Recyclerview部分:调用notifyDataSetChanged()时出现IllegalStateException [重复]

滚动 RecyclerView 时出现“打开的文件太多”

在 Fragment 中使用 RecyclerView 时出现 Kotlin 错误的 Android

Recyclerview cardview Onclick滚动时出现重复效果

Eclipse中使用recyclerview时出现Caused by: java.lang.NoClassDefFoundError: android.support.v7.recyclerview.