0

When working with 4.7 WXGA API 23, I am getting the error of "android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class "

but in higher versions, it is all working well, I have added the code and the xml layout for my issue, don't understand why it is happening so

Code where this error is occurring

@NonNull
@Override
public PostsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_list_layout,
            parent, false);
    return new PostsHolder(v);
}

XML Layout for recyclerview Item

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/shadow_300_50"

        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dp"
            android:padding="5dp"
            android:orientation="horizontal"
            >
            <de.hdodenhof.circleimageview.CircleImageView
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:src="@drawable/profile"
                android:id="@+id/post_user_profile_image"
                android:scaleType="centerCrop"
                android:layout_marginLeft="4dp"
                />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"

                >
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Username"
        android:id="@+id/post_item_username"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="8dp"
        android:textAlignment="textStart"
        android:textSize="16sp"
        android:textStyle="bold"
        android:textColor="@android:color/black"/>


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Time"
        android:id="@+id/post_item_time"
        android:layout_marginLeft="257dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="8dp"
        android:textAlignment="textStart"

        android:textSize="16sp"
        android:textStyle="bold"
        android:textColor="@android:color/black"/>
</RelativeLayout>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Title"
                    android:id="@+id/post_item_title"
                    android:layout_marginLeft="5dp"
                    android:layout_marginTop="5dp"
                    android:textAlignment="textStart"
                    android:textSize="16sp"
                    android:textStyle="bold"
                    android:textColor="@android:color/black"/>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:layout_marginLeft="5dp"
                    android:padding="2dp">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="post_item_Category"
                        android:textColor="@color/colorPrimaryDark"
                        android:textSize="13sp"
                        android:textStyle="bold"
                        android:id="@+id/post_item_Category"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Post_item_SpinnerC"
                        android:textColor="@color/colorPrimaryDark"
                        android:textSize="13sp"
                        android:id="@+id/post_item_SpinnerC"
                        android:layout_marginLeft="7dp"/>



                </LinearLayout>


            </LinearLayout>

        </LinearLayout>


        <TextView
            android:id="@+id/post_item_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="70dp"
            android:layout_marginRight="3dp"
            android:padding="3dp"

            android:text="Post Description"
            android:textColor="@android:color/black"
            android:textSize="14dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:padding="2dp">

            <Button
                android:id="@+id/imageButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Mark"
                app:srcCompat="@drawable/ic_heart_normal"
                android:background="@drawable/button"/>

            <Button
                android:id="@+id/commenting_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="140dp"
                android:text="Comment"
                android:background="@drawable/button"/>


        </LinearLayout>
    </LinearLayout>

</RelativeLayout>
  • is @drawable/profile a vector? – Amir Oct 15 '19 at 12:06
  • @Amir no it is .png(V21) –  Oct 15 '19 at 12:07
  • move the file to drawable folder instead of drawable-v21 – NIKHIL MAURYA Oct 15 '19 at 12:09
  • @NIKHILMAURYA no it didn't worked that way –  Oct 15 '19 at 12:15
  • Where is `shadow_300_50` drawable placed. – Mustansar Saeed Oct 15 '19 at 12:16
  • @MustansarSaeed it is placed in drawable-v24 –  Oct 15 '19 at 12:21
  • that's the issue. Kindly move this `shadow_300_50` to drawable folder as your phone is unable to fetch this. You have declared drawable to be used from API 24 above – Mustansar Saeed Oct 15 '19 at 12:23
  • @MustansarSaeed it is not working that way, it showing error of "android.content.res.resources$notfoundexception: resource id #0x7f060054" –  Oct 15 '19 at 12:31
  • Please post `shadow_300_50`, secondly, kindly remove `shadow_300_50` from `LinearLayout` and check it will start working. Thirdly, you are using some features of `24` that's why it showing this exception, Create default `shadow_300_50` with no shadow and then create other copy for `drawable-24`. There must be drawable for default case as well. – Mustansar Saeed Oct 15 '19 at 12:34
  • can you see anything in preview for android-23? – Rahul Oct 15 '19 at 13:39
  • @user6327816 yes , there is option because i have setup an emulator for the same –  Oct 15 '19 at 15:49
  • Does this image work if you show it in an imageview – NIKHIL MAURYA Oct 15 '19 at 17:36
  • @NIKHILMAURYA yes it does work , but not in the version I mentioned –  Oct 16 '19 at 01:16
  • Please post the whole stacktrack maybe something else is going on, what about Out of memory error when decoding bitmap? – NIKHIL MAURYA Oct 16 '19 at 11:45
  • 1
    @NIKHILMAURYA, i shifted all the drawables of V24 to drawables and it worked finally, thanks for your worthful guidance –  Oct 16 '19 at 12:48
  • @ramneek I am glad it's working but still confused how? Since you are running it on a API 23 so the folder in question should be v23 not v24, also it should have worked when you put this file in drawable folder(without any versions) – NIKHIL MAURYA Oct 17 '19 at 04:56
  • @NIKHILMAURYA yes it is working in drawable folder(without any versions) as I moved all the files to drawable folder(without any versions) –  Oct 17 '19 at 05:12

1 Answers1

0

The problem is that a constructor for a class for creating one of the widgets doesn't exist or can't be found. The LayoutInflater class (in the sdk, ie: Android\Sdk\sources\android-17\android\view\LayoutInflater.java) uses reflection(code introspection) on the widget name to build the widget object. In your case I would guess it is 'de.hdodenhof.circleimageview.CircleImageView', in my case it was 'androidx.appcompat.widget.Toolbar'.

Finding the problem is buried in several layers of exceptions, and only the (pretty useless) 'Binary XML file line #7: Error inflating class...' message appears. I set up Android Studio to break on the android.view.InflateException exception:

enter image description here

The 'ie' InflateException at the bottom contains a java.lang.reflect.InvocationTargetException, which was in turn caused by a android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x7f03008f a=-1} exception (in my case)

In the LayoutInflator code:

enter image description here

Lin 616 is where they embed the exception named 'e' in the InflationException. Using the 'Evaluate Expression' feature of AS, I called 'e.getStackTrace()' which shows where the error really occurred:

enter image description here

StackTrace line 2 shows that it was line LayoutInflater line 587 (seen with red background in code image above) that threw the exception. Looking up to stackTrace line 0, it is the java.lang.reflect.Constructor.constructNative method that threw the original exception.

LayoutInflator line 587 is was trying to instanciate androidx.appcompat.widget.Toolbar (for me) and was looking for a Toolbar constructor with 2 arguments:

1) Context: ref to activity in which the layout was being inflated
2) AttributeSet: the opaque XmlBlock object named 'attrs'

I found this out by using Expression Evaluator on 'this.mConstructorArgs' (seen in line 584 of the code).

So, for me, it could not find a constructor with the Toolbar(Activity, XmlBlock) signature. So I have to look into where Toolbar is defined in by configuration and find out what constructors it has.

The usual 'solution' people give as in this question, is to clear caches, reset support libraries, run with the latest dependencies etc, which don't get you to the root cause, and sometimes don't work.

I'm documenting this here somewhat for my own future problems -- and also as a plea to the Android developers to give clearer error messages. 'Binary XML file line #7: Error inflating' doesn't help much and line 7 of some Binary XML file doesn't give and clues.

@ramneek, 'de.hdodenhof.circleimageview.CircleImageView' is the only unusual widget you are inflating there must be some method or constructor for that class/widget that the inflator can't find.

Ribo
  • 3,363
  • 1
  • 29
  • 35