InflateException: Binary XML file line #8: Error inflating class ImageView
Asked Answered
I

18

42

I know there are other questions on SO asking the same thing. I have already looked at most of them.

Most answers call about memory errors. I can't see how an ImageView which loads just a small icon (24x24 pixels) can make something like this.

But I'd like to post the stacktrace here maybe someone else can identify something I can't and help me.

06-03 22:14:17.367 28590-28590/css.mvp.example.debug W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41e4ad40)
06-03 22:14:17.409 28590-28590/css.mvp.example.debug E/AndroidRuntime: FATAL EXCEPTION: main
Process: css.mvp.example.debug, PID: 28590  android.view.InflateException: Binary XML file line #8: Error inflating class ImageView
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    at css.mvp.example.main.projects.fragments.adapters.PollAnswersAdapter.getView(PollAnswersAdapter.java:38)
    at android.widget.AbsListView.obtainView(AbsListView.java:2255)
    at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
    at android.widget.ListView.onMeasure(ListView.java:1175)
    at android.view.View.measure(View.java:16540)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
    at android.view.View.measure(View.java:16540)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at android.view.View.measure(View.java:16540)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
    at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:668)
    at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:735)
    at android.view.View.measure(View.java:16540)
    at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1075)
    at android.view.View.measure(View.java:16540)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at android.view.View.measure(View.java:16540)
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
    at android.view.View.measure(View.java:16540)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at android.view.View.measure(View.java:16540)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
    at android.view.View.measure(View.java:16540)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
    at android.view.View.measure(View.java:16540)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at android.view.View.measure(View.java:16540)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
    at android.view.View.measure(View.java:16540)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
    at android.view.View.measure(View.java:16540)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1942)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1132)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1321)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725)

Layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal"
android:weightSum="11"
android:paddingTop="16dp" android:paddingBottom="16dp"
android:gravity="center_horizontal"
android:layout_width="match_parent" android:layout_height="wrap_content">

<ImageView
    android:id="@+id/iv_answer_circle"
    android:src="@drawable/ic_dot"
    android:layout_gravity="center" android:layout_weight="2"
    android:layout_marginLeft="16dp" android:layout_marginRight="16dp"
    android:layout_width="0dp" android:layout_height="wrap_content" />

<TextView android:id="@+id/tv_poll_answer"
    android:layout_weight="6"
    android:layout_width="0dp" android:layout_height="wrap_content" />

<TextView android:id="@+id/tv_poll_answer_num"
    android:layout_weight="1"
    android:layout_marginLeft="16dp" android:layout_marginRight="8dp"
    android:layout_width="0dp" android:layout_height="wrap_content" />

<TextView android:id="@+id/tv_poll_answer_percent"
    android:layout_weight="2"
    android:layout_marginRight="16dp"
    android:layout_width="0dp" android:layout_height="wrap_content" />

</LinearLayout>

EDIT 2 : the code of my Adapter class.

public class PollAnswersAdapter extends ArrayAdapter<PollAnswerItem> {

    private Context context;
    private ArrayList<PollAnswerItem> data;


    public PollAnswersAdapter(Context context, ArrayList<PollAnswerItem> data) {
        super(context, -1, data);
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.row_answer, parent, false);

        ImageView ivCircle = (ImageView) rowView.findViewById(R.id.iv_answer_circle);
        TextView tvAnswer = (TextView) rowView.findViewById(R.id.tv_poll_answer);
        TextView tvNums = (TextView) rowView.findViewById(R.id.tv_poll_answer_num);
        TextView tvPercent = (TextView) rowView.findViewById(R.id.tv_poll_answer_percent);

        tvAnswer.setText(data.get(position).getAnswer());
        tvNums.setText(data.get(position).getNum());
        tvPercent.setText(data.get(position).getPercent() + "%");
        tvPercent.setTextColor(ColorTemplate.PREDEFINED_COLORS[position]);

        return rowView;
    }

}
Inadvertent answered 3/6, 2016 at 19:19 Comment(17)
There is more to your stack trace, specifically one or more Caused by stanzas. Please edit your question and post the entire stack trace.Malinowski
That's all. Maybe logcat doesn't displays it all cause it's too big ?Inadvertent
show your XML file alsoBrominate
@Malinowski I tried using logcat from the command line. I get exactly the same message. The next line is W/ActivityManager( 1049): Force finishing activity css.mvp.example.debug/css.mvp.example.main.projects.ProjectsActivityInadvertent
@RakshitNawani updated the question with the xml codeInadvertent
Try to post your whole XML code and tell us what is line #8 in XML fileBrominate
@RakshitNawani line #8 is where ImageView code begins.Inadvertent
@Inadvertent : Not getting what the error is, show us the full XML codeBrominate
@RakshitNawani sorry I haven't edit my question correctly. Now full xml code is visibleInadvertent
There must be more logcat. As the guy said, specially caused by part.Amund
LayoutInflater.createViewFromTag... Are you using a ViewHolder pattern?Impeach
@cricket_007 no I'm not using ViewHolder pattern.Inadvertent
@cricket_007 thanks for editing my code. Somehow I couldn't find what was wrong with so many spaces. I have added the code of my Adapter class. As you can see there's no use of ViewHolder pattern.Inadvertent
I have a hunch that the problem may be with the drawable you set in android:src. Can you remove that from your ImageView and see if you still get the crash?Nephritic
@MarcinKoziński might be. I tried a different drawable and seems ok right now...Inadvertent
By the way, tvNums.setText(item.getNum());... if num is an int, then you'll want tvNums.setText(String.valueOf(item.getNum())); to avoid an error.Impeach
@cricket_007 num is a String. But thanks for pointing this out :)Inadvertent
F
40

The stack trace doesn't show it, but the error during inflation might come from drawable used in the ImageView, in this case:

android:src="@drawable/ic_dot"

OP tested this hunch, by trying a different drawable which "seems ok right now...".

Foison answered 3/6, 2016 at 20:18 Comment(3)
Any idea why a .jpg file is causing this error?Pediform
I have the same problem with two svg's (turned into xml by android studio). I suspect it could be because they have too big width and height (even though I tried changing those parameters inside the files, no luck)Corenecoreopsis
even i have svg file as source. but properties are as follows: android:width="24dp" android:height="24dp" android:tint="#1e8868" android:viewportWidth="24" android:viewportHeight="24"> then why is it happening with me??Yoshikoyoshio
W
72

binary xml file line #0: error inflating class imageview

you have to see if your image has become drawable-v23 or v24 which might be higher than your mobile os level, Make sure to avoid creating drawable version image in project

example In drawable folder ~> "ic_contact(v24)"

Wilma answered 16/12, 2017 at 18:35 Comment(3)
Thanks, I'm fairly new into Android development (2020) and didn't even know about v24, I copy pasted the resoures that had a "v24" into the /drawable folder, and that solved the issue!Heterochromatic
Yes. this was the cause. Thank you. Seems like android studio just copied my drawables into "drawable-v24" instead of "drawable" and I didnt realize until try to run the app on a device that has api 23.Xavier
Yes. this was the cause. It's not any mistake in code.Iamb
F
40

The stack trace doesn't show it, but the error during inflation might come from drawable used in the ImageView, in this case:

android:src="@drawable/ic_dot"

OP tested this hunch, by trying a different drawable which "seems ok right now...".

Foison answered 3/6, 2016 at 20:18 Comment(3)
Any idea why a .jpg file is causing this error?Pediform
I have the same problem with two svg's (turned into xml by android studio). I suspect it could be because they have too big width and height (even though I tried changing those parameters inside the files, no luck)Corenecoreopsis
even i have svg file as source. but properties are as follows: android:width="24dp" android:height="24dp" android:tint="#1e8868" android:viewportWidth="24" android:viewportHeight="24"> then why is it happening with me??Yoshikoyoshio
V
31

If you are using vector drawables, use

app:srcCompat="@drawable/ic_dot"

instead of

android:src="@drawable/ic_dot"
Viridity answered 28/3, 2017 at 0:1 Comment(1)
thanks for answer... After I migrate my application to Android X... I got Error inflating class <unknown> Resources$NotFoundException After I added new ImageView with newly vector drawable.... Oldest Imageview is not give this crash... Imageview with after added Android x cause this error ... I will see result with after applying your answerInappreciable
C
15

What helped me is just copying the image file (.png in my case)

  • from res\drawable-v24

  • to \res\drawable directory.

Will happy if this post will save time to somebody else.

Compensable answered 24/11, 2019 at 13:49 Comment(1)
This worked for me - when trying app in sdk 23 (android 6) for backward compat.Runway
B
8

I had the same problem. The png image for my imageView was causing a class inflateException. This was only happening with my phone running android 6.0.1, but not in my phone running android 7.0. To solve this:

---In Android Studio---

  • Deleted the png image from the drawable folder (I only have the drawable folder visible, not the drawable-v24).
  • Copy the png back to the drawable folder. When doing this, you'll be able to pick to copy it to either the drawable or drawable-v24 folder. Copy the png to both folders
  • Copy the png to both folders (png's must have same name).
  • Once you copy the png to both folders, a new folder with the png's name will show up. Inside it you will see the 2 png's. One with "(v24)" next to it. New folder with name of png's will appear, containing png's
Binturong answered 29/3, 2019 at 4:9 Comment(1)
The PNG only needs to be in one folder, not both.Dipteran
D
5

I also face this kind of problem . the simplest way to solve

Just Put your drawable files in drawable directory, not drawable 24.

Dragonhead answered 4/7, 2019 at 11:21 Comment(0)
S
5

This issue is usually caused when you copy drawables files to drawable-24 folder instead of drawable folder. So, copy all images from drawable-24 folder to drawable.

Salted answered 11/7, 2020 at 12:1 Comment(0)
S
3

using android.support.v7.widget.AppCompatImageView without android:background solved the problem for me.

Sazerac answered 13/6, 2019 at 15:52 Comment(0)
R
2

I was suffering from the same problem today, found the solution! Now whenever you add image file make sure you convert it into xxhdpi by this your app will run on every device!

Rudolph answered 7/4, 2020 at 13:32 Comment(0)
T
1

The following code might be added in Android Manifests-Activity

<activity
android:name=""
--------------
android:theme="@android:style/Theme.Black.NoTitleBar">
</activity>

please remove this theme if you have added.

My problem has been solved after removing this theme. I got these solution after long time.

Tarweed answered 11/8, 2018 at 7:42 Comment(0)
C
1

In my case I had app:srcCompat="@drawable/ic_svg_image" inside ImageView, but it was very complex SVG. Though it showed in Design tab, an application crashed.

I changed it to PNG.

Codel answered 2/4, 2019 at 15:36 Comment(0)
M
1

Don't paste your image into drawable in Android studio, paste it into the drawable folder and clean the project and then run it will work.

Melessa answered 28/5, 2019 at 10:5 Comment(0)
W
1
android:src="@raw/ic_image"

I added the image file to new directory called raw and it solved my problem.

Wilmerwilmette answered 15/8, 2019 at 7:51 Comment(0)
S
1

I also use to face this error most often. The reason behind why this error comes is,

1) In ImageView we have used wrong format of image, or an image when we designed and saved it didn't get save properly. it's extension got broken somehow.
2) One another reason of this error is we have used an xml with drawable. For example, android:src="@drawable/ic_left", here ic_left is an xml file in drawable that we are using.

So, to avoid this kind of error, always use .png format image with ImageView. In my case the error was,

java.lang.RuntimeException: Unable to start activity ComponentInfo{package name of project/ActivtyName}: android.view.InflateException: Binary XML file line #255: Binary XML file line #255: Error inflating class ImageView

Selmore answered 19/12, 2019 at 14:9 Comment(0)
L
1

For me, all the drawables were pasted within the \res\drawable folder instead of the drawable-v24 folder. In spite of this, I was experiencing crashes.

The way I fixed this, was to use an androidx.appcompat.widget.AppCompatImageView instead of a regular ImageView.

Next, I changed the android:src to app:srcCompat.

The app stopped crashing hereon.

Lindsley answered 28/3, 2022 at 5:29 Comment(0)
M
0

Delete the png image from the drawable (24) v folder and put the image in drawable folder visible, not the drawable-v24).

Monodrama answered 12/4, 2021 at 5:12 Comment(0)
W
0

Well in my case it was crashing in only release builds because of proguard, so after i have added this line:

-keep class android.support.v7.widget.** { *; }

in file proguard-rules.pro, the crash has been resolved.

Wil answered 18/4, 2021 at 12:58 Comment(0)
J
0

When you see "Error inflating" with imageview, give it a try to change that ImageView's src file from drawable(24) to simple drawable folder and likewise other files too if there's any. It is because of you API version is bigger then the android version to which you are installing you app.

Johnnyjumpup answered 1/8, 2021 at 10:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.