androidandroid-fragmentsandroid-recyclerviewimageviewandroid-imageview

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


Hi I am getting this below error when I am trying to call a recycler view in Fragment tab from Adaptor class . The error is coming for the Image view. I have searched a lot about it, but no solution worked. My image source is in /res/drawable folder. The layout file is working fine in other Activity class, but giving error in this one only. I have tried to create another layout file without image view and that is working fine. Appreciate any help. Thanks in advance.

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

My XML file:

<?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>

Fragment :

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))
            }

Solution

  • ?attr/selectableItemBackgroundBorderless cannot be resolved since selectableItemBackgroundBorderless is provided by Android SDK.

    Try this instead:

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

    Let me know if it's working.