android.view.InflateException:二进制 XML 文件行 #49:膨胀 class android.widget.ImageView 时出错

android.view.InflateException: Binary XML file line #49: Error inflating class android.widget.ImageView

您好,当我尝试从适配器 class 调用片段选项卡中的回收器视图时,出现以下错误。图像视图出现错误。我已经搜索了很多,但没有解决方案。我的图像源在 /res/drawable 文件夹中。布局文件在其他 Activity class 中工作正常,但仅在这一个中出错。我试图创建另一个没有图像视图的布局文件并且工作正常。感谢任何帮助。提前致谢。

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.pack.account, PID: 6811
    android.view.InflateException: Binary XML file line #46 in com.pack.account:layout/itemrow_lookup_account: Binary XML file line #46 in com.pack.account:layout/itemrow_lookup_account: Error inflating class android.widget.ImageView
    Caused by: android.view.InflateException: Binary XML file line #46 in com.pack.account:layout/itemrow_lookup_account: Error inflating class android.widget.ImageView
    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:852)
        at android.view.LayoutInflater.createView(LayoutInflater.java:774)
        at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:928)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:948)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1002)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
        at com.pack.account.adaptors.ItemAdaptor.onCreateViewHolder(ItemAdaptor.kt:201)
        at com.pack.account.adaptors.ItemAdaptor.onCreateViewHolder(ItemAdaptor.kt:29)
        at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7078)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6235)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
        at androidx.recyclerview.widget.RecyclerView.onMeasure(RecyclerView.java:3540)
        at android.view.View.measure(View.java:27131)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7951)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
        at android.view.View.measure(View.java:27131)
        at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:2362)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.widget.ScrollView.onMeasure(ScrollView.java:701)
        at android.view.View.measure(View.java:27131)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:995)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
        at android.view.View.measure(View.java:27131)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7951)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:27131)
        at androidx.recyclerview.widget.RecyclerView$LayoutManager.measureChildWithMargins(RecyclerView.java:9384)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1653)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
E/AndroidRuntime:     at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
        at android.view.View.layout(View.java:24475)
        at android.view.ViewGroup.layout(ViewGroup.java:7383)
        at androidx.viewpager2.widget.ViewPager2.onLayout(ViewPager2.java:527)
        at android.view.View.layout(View.java:24475)
        at android.view.ViewGroup.layout(ViewGroup.java:7383)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:24475)
        at android.view.ViewGroup.layout(ViewGroup.java:7383)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:24475)
        at android.view.ViewGroup.layout(ViewGroup.java:7383)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:24475)
        at android.view.ViewGroup.layout(ViewGroup.java:7383)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:24475)
        at android.view.ViewGroup.layout(ViewGroup.java:7383)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:24475)
        at android.view.ViewGroup.layout(ViewGroup.java:7383)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:1227)
        at android.view.View.layout(View.java:24475)
        at android.view.ViewGroup.layout(ViewGroup.java:7383)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:4260)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3695)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2618)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:9971)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1010)
        at android.view.Choreographer.doCallbacks(Choreographer.java:809)
        at android.view.Choreographer.doFrame(Choreographer.java:744)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:995)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8633)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
     Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 35: TypedValue{t=0x2/d=0x7f030341 a=-1}
        at android.content.res.TypedArray.getDrawableForDensity(TypedArray.java:997)
        at android.content.res.TypedArray.getDrawable(TypedArray.java:981)
        at android.view.View.<init>(View.java:6301)
        at android.widget.ImageView.<init>(ImageView.java:198)
        at android.widget.ImageView.<init>(ImageView.java:193)
        at android.widget.ImageView.<init>(ImageView.java:189)
            ... 96 more

我的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/llMain"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:padding="10dp">

    <TextView
        android:id="@+id/tvName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:textColor="@android:color/black"
        android:textSize="16sp"
        tools:text="Name" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="5dp"
        android:layout_marginEnd="5dp"
        android:textSize="18sp"
        android:textStyle="bold"
        tools:text="," />
    <TextView
        android:id="@+id/tvPlace"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.75"
        android:textColor="@android:color/black"
        android:textSize="16sp"
        tools:text="Place" />

    <ImageView
        android:id="@+id/ivselect"
        android:layout_width="0dp"
        android:layout_height="35dp"
        android:layout_weight="0.35"
        android:layout_gravity="end"
        android:contentDescription="@string/lbl_select"
        android:foreground="?attr/selectableItemBackgroundBorderless"
        android:visibility="invisible"
        android:src="@drawable/ic_action_check" />

    <!--ImageView
        android:id="@+id/ivDelete"
        android:layout_width="50dp"
        android:layout_height="35dp"
        android:layout_gravity="end"
        android:contentDescription="@string/lbl_delete"
        android:foreground="?attr/selectableItemBackgroundBorderless"
        android:src="@drawable/ic_action_delete" /-->


</LinearLayout>

片段:

val context = requireActivity().applicationContext

private fun getItemsList(rectype: String, context: Context): ArrayList<ModelClass> {
        
            //creating the instance of DatabaseHandler class
            val databaseHandler = DatabaseHandler(context)
            //calling the viewEmployee method of DatabaseHandler class to read the records

            @Suppress("UNCHECKED_CAST")
            return databaseHandler.getAll() as ArrayList<ModelClass>
        

        return ArrayList()
    }
    private fun setupListofDataIntoRecyclerView(view: View,rectype: String, context: Context) {

        val itemlist = getItemsList(rectype, context)
        if (itemlist.size > 0) {

            view.rvAcList.visibility = View.VISIBLE

            // Set the LayoutManager that this RecyclerView will use.
            view.rvAcList.layoutManager = LinearLayoutManager(context)
            // Adapter class is initialized and list is passed in the param.
            itemAdapter = ItemAdaptor(context, itemlist, rectype)
            // adapter instance is set to the recyclerview to inflate the items.
            view.rvAcList.adapter = itemAdapter
        } else {

            view.rvAcList.visibility = View.GONE
        }
    }

ItemAdaptor onCreateViewHolder:

if(context is Application){
                return ViewHolder(LayoutInflater.from(context).inflate(R.layout.itemrow_lookup_one, parent, false))
            }
            else {
                return ViewHolder(LayoutInflater.from(context).inflate(R.layout.itemrow_one, parent, false))
            }

?attr/selectableItemBackgroundBorderless 无法解析,因为 selectableItemBackgroundBorderless 由 Android SDK 提供。

试试这个:

android:foreground="?android:attr/selectableItemBackgroundBorderless"

让我知道它是否有效。