Android view inflate error

Error inflating class ImageView

I’ve been getting InflateException/ClassNotFoundException error intermittently. I’ve seen similar errors before in SO but they were caused by spelling errors. I spelled ‘ImageView’ correctly so I don’t know what’s causing the error.

The code where the error occurs is:

Here’s the layout xml:

Here is the error log:

18 Answers 18

I had the same problem. my problem has occurred because I just had used images (icons) in the «drawable-v24» folder. I solved it by copying them into «drawable» folder

I came across this same problem recently and was able to solve it using app:srcCompat=»@drawable/ic_black_image_24″

The documentation of appcompat:srcCompat states that —

It sets a drawable as the content of this ImageView. Allows the use of vector drawable when running on older versions of the platform.

Hope this helps if anyone stumbles upon the same problem.

For me , i copy my image to Folder drawable-v24 and compiler didnt find it

Change view of Android studio to project view

and cut and paste your image to folder drawable

I had the same issue, because I copied my png files to drawables while on Android view, and it automatically copied to drawable-v24, but you need to copy your files to drawable, you can navigate to your project and then res/drawables and paste it manually, or change your view to project view the past it in the drawable folder

I myself had this problem because my image was in darawable-v24 directory. so when I wanted to use older api, the app was crashing. so I suggest to check whether you’re resource is in correct directory or not.

It’s an old question but I just came across the same problem. In my case it was caused by using the android:tint attribute with an selector rather than a color :

While this works without a problem a SDK 21+ it crashes in older versions.

Switching to AppCompatImageView and to app:tint solved the problem:

For me, inside one of my layout.xml files, I had

and inside strings.xml, I had

and so it was showing this in Android Studio:

Источник

android.view.InflateException: Binary XML file line #12: Error inflating class

I am receiving many errors of kind displayed in the subj. These errors seems to be occasional and I cannot reproduce them. From stack I can learn that such error may occurs for my different layout resources. The line of XML is also varying.

Can anybody explain why this error occurs? And what I can do to fix this problem?

Here is a result of XML, however such error occurs in other xmls

32 Answers 32

The inflate exception is not actually the problem but really comes from another deeper issue in your layout that is then wrapped in an InflateException . A common issue is an out of memory exception when trying to inflate an ImageView loading a drawable resource. If one of these resources has a high pixel resolution it would take a lot of memory causing then an inflate exception.

So basically verify that the pixel resolution in all your image drawables is just the minimum necessary for your layout.

This can also happen if you use a VectorDrawable using support library and forgot to use app:srcCompat instead of android:src

Exact error is: Binary XML file line #XX: Error inflating class ImageView

ViewFlipper loads all images into memory during layout inflating. Because my images are big it takes many memory, I replaced ViewFlipper with ImageSwitcher which can change the images with animation like ViewFlipper but it loads only one image at the time.

Читайте также:  Как убрать оповещение android

I had the same error when creating custom view with only one constructor, try to define all constructor for your custom views.

I know the question is already answered but still I’m posting with thought that may someone run into this kind of problem.

In my case problem is i’m loading my application to phone which refer layouts from res/layout/ folder and values for @dimens from res/values/dimens here it’s font_22 which it’s trying to access and it’s define in res/values-xlarge/dimens.

I’m actually updating UI of existing project.

I ran into this problem because I’m using IDE Eclipse where I ctrl+space for hint while writing xml for layout folder it displays all values from values as well as values-xlarge folder regardless of for which folder I’m writing.

I also know that the values in both files should be same to mapped for different screens.

Hope this may help someone run into this kind of silly problem.

I encountered the same bug and found the root reason is:

Use Application Context to inflate view.

Inflating with Activity Context fixed the bug.

I found the same error, and it took two days to identify what the error was.

The error was simply because I was trying to use: android:background

Rather than: app:srcCompat

In your case, I believe this is it

I recommend using it this way

add this to your build.gradle app

I hope this helps.

I had this error because i selected theme as Material theme. But as i was trying to run app on 4.4.2 it gave this error.

Solution : Select Theme_holo as theme

My picture size is only 14Kb but the problem is that the designer gave me 1011px x 1819px (resolution too high hence causing the InflateException)

In case someone gets similar issues, I had such an issue while inflating the view:

View.inflate(getApplicationContext(), R.layout.my_layout, null)

fixed by replacing getApplicationContext() with this

For me, the error message was actually insufficient in the log cat, so here’s what I did to figure out what caused the problem:

(In the log cat error message, it said the error occurred while inflating a specific layout in my HomeFragment.java)

  1. I put a breakpoint just before the layout was inflated
  2. I ran the application in debug mode until it reached that specific breakpoint
  3. I selected the line with my cursor and ran Evaluate expression on it:
    • Run > Evaluate Expression or alt — F8
  4. The result showed me more information about the source of the problem, which in my case, was a drawable file using tools:targetApi=»lollipop» (the bug only occurred on older devices).

For me, my issue was that I was

To a LinearLayout background, once I removed it the test passed.

This link may help you. Try checking in your manifest for problems. If you can get it to happen again, post your entire stack trace so that we can see what the error actually is.

EDIT: I’m sure you’ve checked this, but what is on line 12 of the XML file you use for the TourActivity layout?

I know it is an answered question, but I do not see the cause that I faced.

It was that Android Studio placed my drawables in /drawable-V24 My emulator is API 23. So, eventually it can not find it.

The solution was to move all drawables to /drawable folder (no -24).

You should copy the image files from the «drawable-24» folder to the «drawable» folder.

I had the same error and I solved moving my drawables from the folder drawable-mdpi to the folder drawable. Took me some time to realize because in Eclipse everything worked perfectly while in Android Studio I got these ugly runtime errors.

Edit note: If you are migrating from eclipse to Android Studio and your project is coming from eclipse it may happen, so be careful that in Android Studio things a little differs from eclipse.

Читайте также:  Как разбить память андроид

In my case,this error happen when I use the Floating action button and set android:backgroundTint=»#000″ . Then just don’t set backgroundTint and problem solved. Hope it’s helpful for you.

I just solve this issue just delete your images from drawable(v24) and just paste it in drawable folder this works for me.

I had this problem just now and managed to figure out what it was. Was referencing a colour in my values that was causing problems. So defined it manually instead of using one from the dropdown suggestions.Then it worked!

I had same error when I ran my app on API lvl 16 (26 was fine). It was because I used ?attrr/colorAccent in my shape drawable:

Replacing that with a regular color fixed my problem.

i had the same issue and it solved by removing drawable-v24 with (24) in front of images in drawable folder and replacing them with ordinary drawables.

I know this thread is old, but still answering it so that no-one else should spend sleepless nights.

I was refactoring an old project, whose layout files all contained hardcoded attributes such as android:maxLength = 500 . So I decided to register it in my res/dimen file as 500 .

Finished refactoring almost 30 layout files with my res-value. Guess what? the next time I ran my project it started throwing the same InflateException .

As a solution, needed to redo my all changes and keep all-those values as same as before.

TLDR;

step 1: All running good.

step 2: To boost my maintenance I replaced android:maxLength = 500 with 500 and android:maxLength = @dimen/max_length , that’s where it all went wrong(crashing with InflateException ).

step 3: All running bad

step 4: Re-do all my work by again replacing android:maxLength = @dimen/max_length with android:maxLength = 500 .Everything got fixed.

Источник

android.view.InflateException Error inflating class android.webkit.WebView

In Lollipop (API 22) every time in my application I show a webview the application crashes. I have multiple crashes in my android developer console related to this event.

No need to say that it works on Android 4, 6 and 7.

Reading the stack trace (posted at the end of this post), something bugs me

I searched in the generated R.java without any luck, obviously because the ID does not exists, but it was worth a try.

Googling around the problem seems to be related to how lollipop handles the webview. I started a fresh AVD with lollipop based on a device I found on the crash reporter in GDC, and I can reproduce the problem.

Full stack trace:

20 Answers 20

If you use androidx.appcompat:appcompat:1.1.0 , try androidx.appcompat:appcompat:1.0.2 instead. it seems that 1.1.0 doesn’t fix the bug with WebView in Android 5.1.1 .

Feb-2020 update: Reverting to 1.0.2 stopped working for many people (including my app), but using the current version of androidx.appcompat:appcompat:1.2.0-alpha02 did fix the crash. (I was seeing it on a Huawei P8 Lite running Android 5.0 during Google’s automated «Pre-launch report» testing).

Jun-2020 update: There are newer releases available than the one mentioned in the Feb-2020 update, you can see the currently available versions here:

If you are using androidx.appcompat:appcompat:1.1.0 and don’t want to downgrade to androidx.appcompat:appcompat:1.0.2 or upgrading to androidx.appcompat:appcompat:1.2.0-alpha03 , there’s another solution which is described in this comment on the Google Issue Tracker.

I noticed that after calling applyOverrideConfiguration, Context.getAssets() and Context.getResources().getAssets() are not returning the same AssetManager object. The AssetManager returned from Context.getAssets() can’t access resources in other packages (including the system WebView package), causing WebView to crash. If I override Context.getAssets() to return getResources().getAssets() , the problem is gone.

Based on that comment, you can override the getAssets() in the WebView’s Activity so that it returns getResources().getAssets() instead to solve the issue.

Warning: this workaround might also break some things; see the comments for details

If you’d like to inflate the WebView from an XML layout, you can wrap it in a nice little subclass (based on ikostet’s answer):

Читайте также:  Учет товара для android

EDIT: now even nicer with Kotlin

my advice is to use custom/new Configuration only when «original one» is causing problems, so on Lollipop only. @SpaceBizon code work well till Android 8.x, on 9 and Q (currenly beta) every select/dropdown press won’t show AlertDialog picker, instead of that memory leak occurs. below fixed getFixedContext method with «iffed» proper version code

If you are using androidx.appcompat:appcompat:1.1.0, either change to androidx.appcompat:appcompat:1.0.2 or if you want to use DayNight Theme, override applyOverrideConfiguration in your activity as follows. (Note: This requires app restart while switching from Dark Theme to Light Theme and vice versa).

If you are not relying on DayNight theme switching (or other UiMode events), you can add android:configChanges=»uiMode» to the webview activity manifest to prevent AppCompatDelegate updating the Resources configuration and thereby messing up the webview inflation.

Try use for creating webview:

And one more try to fix the problem. Should override this method in your activity:

I was able to reproduce the crash on API 21 in the emulator.

So I tried to add this to the implementation, as described in the docs:

Adding appcompat-resources did not fix the issue.

Maybe a future version will, but I wanted to mention that there seems to be a complementary resource library that is supposed to address this issue. So when trying to fix this with a new version of appcompat , add the appcompat-resources library with the same version.

Reverting to androidx.appcompat:appcompat:1.0.2 did fix the issue as a workaround.

The below code will fix the problem. Please add it in your Activity :

I used the latest version from this source. Please check and use your own based on what you think is best.

In case you want to skip, below implementation solves the issue:

Just as a side note, I struggled for a while even though I downgraded to 1.0.2, just because I seemed to have a transitive dependency to appcompat:1.1.0 via androidx.constraintlayout:constraintlayout:2.0.0-beta3. Once I downgraded that to constraintlayout:1.1.3 everything worked fine

(According to https://issuetracker.google.com/issues/141351441 issue will be fixed soon with appcompat:1.2.0-alpha02)

戴文锦 was right for me too. But downgrading just androidx.appcompat:appcompat:1.1.0 to 1.0.2 did not succeed.

I downgraded all my previously upgraded androidx versions from

A reminder that Googles code is by far not bug free and well tested and that someone should never upgrade the versions flippantly.

AppCompat 1.2.0-alpha02 was released today, so you can use

which will fix WebView issues on Lollipop.

I was able to reproduce this issue on a Nexus 7 API 22. The issue is fixed by updating the tablet’s version of Android System WebView from version 39 (2237560-arm) to version 79.0.3945.136

This isn’t in my control for a user to do this, but it is a solution if you need to help someone use your app immediately.

The issue was solve for my by downgrading to implementation ‘androidx.appcompat:appcompat:1.0.2’

In my case a problem was in method

of a MainActivity. I don’t know why it affects WebView . getUserId() is neither overriden, nor public method of the Activity class. If I rename or remove this method, WebView starts to open. Also trying to change

leads to the same exception. I understood that when had seen an exception: Caused by: java.lang.SecurityException: Permission Denial: null asks to run as user 123456 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL or android.permission.INTERACT_ACROSS_USERS . In that case user=123456 was in fact my authorized user, that was used in some requests, not in WebView . I suspect Android uses userId as a local user_id for its processes.

As many other people noticed we could use appcompat:1.1.0 or 1.0.2 and extend WebView class. In emulator 21 without Google Play Services this WebView will crash on long touch over text labels, but on typical devices everything is allright.

Источник

Оцените статью