0

Application crashes with the following exception:

android.view.InflateException: Binary XML file line #185: Binary XML file line #15: Error inflating class ImageButton

But Android Studio shows no layout errors, layout preview of the file that is responsible for the crash is properly rendered and the application can be compiled and run.

Crashing ImageButton:

<ImageButton
    android:id="@+id/kasablok_pager_btn_left"
    android:layout_width="74dp"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    app:srcCompat="@drawable/ic_chevron_right_black_24dp"
    android:rotation="180"
    tools:ignore="ContentDescription" />

The source drawable exists and is click-through to the file. There are no errors or warnings in the preview window to indicate a possible problem.
vectorDrawables.useSupportLibrary = true is also properly set in my build.gradle file.

2 Answers 2

4

Answer: the ic_chevron_right_black_24dp.xml file was located in the specific res/drawable-v24 folder instead of the common res/drawable. This way the id was only available on API 24+. Moving the file into the latter folder did solve the problem. My phone ran Android M (API 23) and that's why it crashed. The ic_chevron_right_black_24dp id could not be found.

How did the file end up in the wrong directory without my knowledge?
I copied the ic_chevron_right_black_24dp.xml file over from another project. What I did was to paste the file into the drawable resource directory while viewing the project as Android (the top left dropdown that allows you to choose how to represent your project in the left navigation panel). Android studio somehow decided to put the file into the specific drawable-v24 instead of the general drawable folder.

Why didn't Android Studio show any errors and why was the preview properly inflated?
The layout preview was properly inflated because the preview and also the compileSdkVersion was set to android-P. This way the preview was rendered as if it was on android-P API, where the resource existed.

How to tackle InflateException in your application?
When you see an InflateException, this exception itself doesn't provide any usable information about your problem. It can be caused by a variety of different independent things. You must look deeper. InflateException is not the cause - it is the effect caused by something else.

In my case the whole stack trace looked like this:

android.view.InflateException: Binary XML file line #185: Binary XML file line #15: Error inflating class ImageButton
    at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:2335)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1423)
    .....

 Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class ImageButton
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
    .....
 Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f070070
    at android.content.res.Resources.getValue(Resources.java:1364)
    at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:328)
    at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:193)
    at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:186)
    at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
    at android.support.v7.widget.AppCompatImageHelper.loadFromAttributes(AppCompatImageHelper.java:58)
    at android.support.v7.widget.AppCompatImageButton.<init>(AppCompatImageButton.java:78)
    at android.support.v7.widget.AppCompatImageButton.<init>(AppCompatImageButton.java:68)
    at android.support.v7.app.AppCompatViewInflater.createImageButton(AppCompatViewInflater.java:201)
    at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:121)  

That's more usable. The trace shows that the InflateException was triggered by a Resources$NotFoundException and this was the real culprit.

Sign up to request clarification or add additional context in comments.

Comments

0

I solved this error, move the file(Image) from /res/drawable-v24 to the general folder /res/drawable, this is folder for any versions, and handle it with app:src in the layout code XML.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.