android.view.InflateException Error inflating class android.webkit.WebView
Asked Answered
S

21

152

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

Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2040003

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:

android.view.InflateException: Binary XML file line #7: Error inflating class android.webkit.WebView
                  at android.view.LayoutInflater.createView(LayoutInflater.java:633)
                  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
                  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
                  at it.artecoop.ibreviary.WebViewFragment.onCreateView(WebViewFragment.java:67)
                  at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
                  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
                  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1682)
                  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:541)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5254)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
               Caused by: java.lang.reflect.InvocationTargetException
                  at java.lang.reflect.Constructor.newInstance(Native Method)
                  at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
                  at android.view.LayoutInflater.createView(LayoutInflater.java:607)
                  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55) 
                  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682) 
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741) 
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
                  at it.artecoop.ibreviary.WebViewFragment.onCreateView(WebViewFragment.java:67) 
                  at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087) 
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113) 
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295) 
                  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801) 
                  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1682) 
                  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:541) 
                  at android.os.Handler.handleCallback(Handler.java:739) 
                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                  at android.os.Looper.loop(Looper.java:135) 
                  at android.app.ActivityThread.main(ActivityThread.java:5254) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
               Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2040003
                  at android.content.res.Resources.getText(Resources.java:299)
                  at android.content.res.Resources.getString(Resources.java:385)
                  at com.android.org.chromium.content.browser.ContentViewCore.setContainerView(ContentViewCore.java:684)
                  at com.android.org.chromium.content.browser.ContentViewCore.initialize(ContentViewCore.java:608)
                  at com.android.org.chromium.android_webview.AwContents.createAndInitializeContentViewCore(AwContents.java:631)
                  at com.android.org.chromium.android_webview.AwContents.setNewAwContents(AwContents.java:780)
                  at com.android.org.chromium.android_webview.AwContents.<init>(AwContents.java:619)
                  at com.android.org.chromium.android_webview.AwContents.<init>(AwContents.java:556)
                  at com.android.webview.chromium.WebViewChromium.initForReal(WebViewChromium.java:311)
                  at com.android.webview.chromium.WebViewChromium.access$100(WebViewChromium.java:96)
                  at com.android.webview.chromium.WebViewChromium$1.run(WebViewChromium.java:263)
                  at com.android.webview.chromium.WebViewChromium$WebViewChromiumRunQueue.drainQueue(WebViewChromium.java:123)
                  at com.android.webview.chromium.WebViewChromium$WebViewChromiumRunQueue$1.run(WebViewChromium.java:110)
                  at com.android.org.chromium.base.ThreadUtils.runOnUiThread(ThreadUtils.java:144)
                  at com.android.webview.chromium.WebViewChromium$WebViewChromiumRunQueue.addTask(WebViewChromium.java:107)
                  at com.android.webview.chromium.WebViewChromium.init(WebViewChromium.java:260)
                  at android.webkit.WebView.<init>(WebView.java:554)
                  at android.webkit.WebView.<init>(WebView.java:489)
                  at android.webkit.WebView.<init>(WebView.java:472)
                  at android.webkit.WebView.<init>(WebView.java:459)
                  at java.lang.reflect.Constructor.newInstance(Native Method) 
                  at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
                  at android.view.LayoutInflater.createView(LayoutInflater.java:607) 
                  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55) 
                  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682) 
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741) 
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
                  at it.artecoop.ibreviary.WebViewFragment.onCreateView(WebViewFragment.java:67) 
                  at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087) 
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113) 
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295) 
                  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801) 
                  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1682) 
                  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:541) 
                  at android.os.Handler.handleCallback(Handler.java:739) 
                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                  at android.os.Looper.loop(Looper.java:135) 
                  at android.app.ActivityThread.main(ActivityThread.java:5254) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
Steelmaker answered 7/12, 2016 at 18:55 Comment(6)
This looks like a system WebView problem. I don't know if/when the emulators get updates to that. Have you tried on hardware?Merrilee
@commonsware no I don't have any device right now with lollipop. But the crash reporter is showing multiple entries for this.Steelmaker
This question was from 2016 but this issue is surfacing again in late 2019. If you arrived here from a web search, make sure to pick answers that applies to you.Gaskill
I don't have physical device of API level 21. I tried running app on Genymotion and Android emulators with API level 21 but it crashes! Anyone here let me know whether it will work on Physical Device! Now my app is at risk!Determinism
For me, Google Play reported the problem on a Huawei P8 Lite running Android 5.0 but it didn't surface on a real Samsung Note 4 running Android 6.0.1 or a Samsung Note 8 tablet running Android 4.4.2.Dicast
Recently I get the crash from Android 10, mostly from Samsung user. Anyone have the same trouble ?Gibraltar
F
193

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:

Fruma answered 17/9, 2019 at 5:54 Comment(15)
Is there a bug fix for keeping 1.1.0 and fixing this? We are reliant on some things not present in 1.0.2 and it seems like some alpha or beta version of 1.1.0 don't have this bug.Headboard
Also am not sure that's the problem, I have a branch with beta01 that works fine with 5.1 and my branch with 1.1.0 doesn't, if i revert my 1.1.0 to beta01 it still doesn't work, other than that the gradle files are the same but i'm not sure what is the main difference since its quite a big branchHeadboard
@PampaZiya I tried to use beta01 version. It's OK. About what you said “if i revert my 1.1.0 to beta01 it still doesn't work” , try clean? What we can do is wait patiently for Google to release a new version.Hoi
Yes a clean fixed it thank you ! I am using beta01 and everything seems fine. I'll keep using it cause there's no obvious benefit of using 1.1.0 anyway.Headboard
There's an issue related with this regresion: issuetracker.google.com/issues/141132133Herefordshire
Thanks, this also seems to fix the getDefaultVideoPoster crash.Dagnah
Having just finished migrating to androidx and facing this issue for API 22 only, it does work.Trigeminal
androidx.appcompat:appcompat:1.1.0-beta01 works for me tooPitcher
yeah works well in android Lollipop without using https://mcmap.net/q/57666/-android-view-inflateexception-error-inflating-class-android-webkit-webview workaround which causing other issues and also https://mcmap.net/q/57666/-android-view-inflateexception-error-inflating-class-android-webkit-webview one works but some condition don't know what are the issue downgrading to beta01Lynch
As today, the downgrade it's not working, in my case, this solution from issuetracker.google.com/issues/141132133 works: "Maybe the most simple way to solve this issue is to force Gradle to set the version to 1.1.0-rc01. Add this to your gradle file. configurations.all { resolutionStrategy { force 'androidx.appcompat:appcompat:1.1.0-rc01' } }"Tops
@Fruma @Tops considering 2 options: using androidx.appcompat:appcompat:1.2.0-alpha02 or androidx.appcompat:appcompat:1.1.0-rc01, which one is more stable and less risky? personally I think rc versions are more stable comparing to alpha versions, and so I decided to use androidx.appcompat:appcompat:1.1.0-rc01.Show
@Show have you tried overriding the getAssets() in the Webview's Activity to return getResources().getAssets() while still using androidx.appcompat:appcompat:1.1.0?Tokay
androidx.appcompat:appcompat:1.2.0-alpha03 works tooDaedalus
upgrading to implementation 'androidx.appcompat:appcompat:1.2.0-beta01 solved the issue, is there any side effects of it on the above versions(6.0 and above)?Lynch
after updating androidx.appcompat:appcompat the issue was gone for me for sometime but today the issue reappeared after updating the dependency to androidx.appcompat:appcompat:1.3.0-alpha02Batson
T
53

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.

Java

@Override
public AssetManager getAssets() {
    return getResources().getAssets();
}

Kotlin

override fun getAssets(): AssetManager {
    return resources.assets
}
Tokay answered 29/1, 2020 at 7:1 Comment(2)
Hi, if this was true, how would this be fixed with a release of appCompact which is unrelated to the AssetManager and the webview itself?Rambler
Looking at this changelist it seems that some webview versions are having issues with the applyOverrideConfiguration calls inside the AppCompat library.Tokay
S
46

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

public class LollipopFixedWebView extends WebView {
    public LollipopFixedWebView(Context context) {
        super(getFixedContext(context));
    }

    public LollipopFixedWebView(Context context, AttributeSet attrs) {
        super(getFixedContext(context), attrs);
    }

    public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(getFixedContext(context), attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(getFixedContext(context), attrs, defStyleAttr, defStyleRes);
    }

    public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr, boolean privateBrowsing) {
        super(getFixedContext(context), attrs, defStyleAttr, privateBrowsing);
    }

    public static Context getFixedContext(Context context) {
        return context.createConfigurationContext(new Configuration());
    }
}

EDIT: now even nicer with Kotlin

class LollipopFixedWebView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
    defStyleRes: Int = 0
) : WebView(context.createConfigurationContext(Configuration()), attrs, defStyleAttr, defStyleRes)
Sanguinary answered 28/2, 2018 at 8:21 Comment(13)
I found the same problem and used this solution to fix it, thank youAngelangela
Updating the Android WebView on the device also fixes the issueDrucilladrucy
@displayname Might not be an option for those running it on the AVDNevil
Still a problem in 2019 and it works, but I don't have idea why/how, it would be good if this is explained. Also I get the warning Is never used on the 4th constructor "public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {"Antigorite
Just a word of caution about this workaround: instantiating a WebView with a blank Context can lead to unintended side effects. For example, ActionModes from the WebView will probably no longer work (i.e. long-pressing to highlight text).Pammie
@displayname Are you sure it is working fine on pysical device!Because I don't have physical device of API level 21!Determinism
Yes. I am. I don't have a device with with OS 5/ API21. I setup an VD with google play and it worked fine on OS 5 and OS 6.Drucilladrucy
As @DmitryBrant said be very careful when implementing this because this breaks a few things, including drop down fields and long pressing to highlight text!Campania
This workaround breaks <select> tags and input fields inside the webview!Griffey
FYI on at least one Samsung 4.1.2 device this can lead to a NoSuchMethodError for android.content.Context.createConfigurationContextDagnah
Just realized that is because it was added on API 17.Dagnah
@DmitryBrant Correct now Dropdown crashing in webview , Is there any fix for this #58710901Lynch
This cause that keyboard is not snown when clicked into an input.Thuja
R
37

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

public class LollipopFixedWebView extends WebView {

    public LollipopFixedWebView(Context context) {
        super(getFixedContext(context));
    }

    public LollipopFixedWebView(Context context, AttributeSet attrs) {
        super(getFixedContext(context), attrs);
    }

    public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(getFixedContext(context), attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(getFixedContext(context), attrs, defStyleAttr, defStyleRes);
    }

    private static Context getFixedContext(Context context) {
        if (Build.VERSION.SDK_INT >= 21 && Build.VERSION.SDK_INT < 23) // Android Lollipop 5.0 & 5.1
            return context.createConfigurationContext(new Configuration());
        return context;
    }
}
Rajasthan answered 16/5, 2019 at 12:38 Comment(0)
T
22

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

override fun applyOverrideConfiguration(overrideConfiguration: Configuration?) {
        if (Build.VERSION.SDK_INT in 21..25 && (resources.configuration.uiMode ==  applicationContext.resources.configuration.uiMode)) {
                return
        }
        super.applyOverrideConfiguration(overrideConfiguration)
}
Turbinate answered 27/9, 2019 at 9:12 Comment(8)
This workaround works, but shouldn't it be for 21..22, instead of 21..25? The problem doesn't seem to occur in API 23+.Pammie
This is the revision that caused this crash - android.googlesource.com/platform/frameworks/support/+/… . So, according to the code, the crash will occur in API 21..25 in the devices in which Google Play Store is not installed and Android Webview Version is <50.Turbinate
Downgrading to androidx.appcompat:appcompat:1.0.2 do the trick, thanksTops
My workaround in java that seems to work for me is like this (sorry for the horrible formatting but it's not a new answer just a port): @Override public void applyOverrideConfiguration(Configuration overrideConfiguration) { if (Build.VERSION.SDK_INT >= 21 && Build.VERSION.SDK_INT <= 25) { return; } super.applyOverrideConfiguration(overrideConfiguration); }Stilliform
@ARMythiliSaran why do you say that this only occurs in "the devices in which Google Play Store is not installed and Android Webview Version is <50" ?Either
@Either I have verified on physical devices(Android 5.1) I own. On devices with google play store, if i update webview app, the crash wouldn't occur. On the other devices there is no way to update the webview unless you install latest webview apk yourself. The lowest webview apk i could get my hands on was version 54 and it didn't crash in that version either. So, I decided to go into production with the above code applied if webview<50. Haven't received any crash reports related to this till date. Seems like it was fixed recently - issuetracker.google.com/issues/141351441.Turbinate
What is AppConstants.appContext.resources.configuration.uiMode ?Augustus
@Augustus Sorry for not keeping it clear. AppConstants.appContext is the application context. Edited the answer accordingly.Turbinate
A
21

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.

Alphonse answered 3/11, 2019 at 20:13 Comment(3)
This worked for me. I need ConstraintLayout 1.1.3, which requires appcompat 1.1.0, so reverting was not an option.Mcmahan
Downgrading to 1.0.2 made no difference to me, but this method worked.Malamut
awesome solution!Stapes
H
17

Try use for creating webview:

mWebView = new WebView(getActivity().createConfigurationContext(new Configuration()));
Harappa answered 18/1, 2017 at 14:17 Comment(2)
and my xml file ?Finella
This solved my problem, without downgrading any library versionReubenreuchlin
I
11

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

    @Override
    public void applyOverrideConfiguration(final Configuration overrideConfiguration) {
        if (Build.VERSION.SDK_INT >= 21 && Build.VERSION.SDK_INT < 25) {
            overrideConfiguration.uiMode &= ~Configuration.UI_MODE_NIGHT_MASK;
        }
        super.applyOverrideConfiguration(overrideConfiguration);
    }
Ish answered 20/11, 2019 at 12:20 Comment(0)
W
6

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:

dependencies {
    def appcompat_version = "1.1.0"

    implementation "androidx.appcompat:appcompat:$appcompat_version"
    // For loading and tinting drawables on older versions of the platform
    implementation "androidx.appcompat:appcompat-resources:$appcompat_version"
}

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.

Wrigley answered 4/10, 2019 at 14:17 Comment(3)
I couldn't revert "androidx.appcompat:appcompat-resources:$appcompat_version" to version 1.0.2. "ERROR: Failed to resolve: androidx.appcompat:appcompat-resources:1.0.2"Taxidermy
appcompat-resources is not available for 1.0.2. See my updated answer.Wrigley
Thank you so much, it worked. I hope that in future versions solve this.Taxidermy
C
6

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

@Override 
public AssetManager getAssets() {
    return getResources().getAssets(); 
}
Coalition answered 11/4, 2020 at 7:35 Comment(0)
P
5

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:

implementation 'androidx.appcompat:appcompat:1.2.0'
Phosphene answered 14/5, 2020 at 20:21 Comment(1)
Still happening, even in 'androidx.appcompat:appcompat:1.3.0'Bulge
M
3

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)

Myles answered 28/12, 2019 at 9:18 Comment(0)
A
2

戴文锦 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

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.google.android.material:material:1.1.0-alpha10'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.recyclerview:recyclerview:1.1.0-beta04'
implementation 'androidx.viewpager2:viewpager2:1.0.0-beta04'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.vectordrawable:vectordrawable:1.1.0'
implementation 'androidx.preference:preference:1.1.0'
implementation 'androidx.core:core:1.2.0-alpha04'

back to

implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.google.android.material:material:1.1.0-alpha09'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.recyclerview:recyclerview:1.1.0-beta03'
implementation 'androidx.viewpager2:viewpager2:1.0.0-beta03'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.vectordrawable:vectordrawable:1.1.0-rc01'
implementation 'androidx.preference:preference:1.1.0-rc01'
implementation 'androidx.core:core:1.2.0-alpha03'

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

Alphonse answered 27/9, 2019 at 6:45 Comment(4)
Did you downgraded androidx.appcompat:appcompat to 1.1.0-rc01? Not to 1.0.2?Chestnut
Tryed it with 1.0.2 but no success. Must be one of the other dependencies in combination with appcompat.Alphonse
Simply downgrading androidx.appcompat:appcompat worked for me. To future readers: make sure to "gradle sync" and confirm it's not sufficient for you, before downgrading more stuff.Gaskill
After downgrading it will not be superfluous to check the versions of dependencies in the project https://mcmap.net/q/55178/-how-do-i-show-dependencies-tree-in-android-studioChestnut
D
2
/**
 * Customized WebView to avoid crashing on Android 5 and 6 (API level 21 to 23)
 * If the Android System WebView is old and is not updated, the WebView view inflation fails.
 * To reproduce the issue, try on OS 5 or 6 with Android System WebView 72.0.3626.76 (this version is just a reference point from being which we saw no crashes)
 */
class BrilliantWebView : WebView {

   companion object {
       private fun getBrilliantContext(context: Context?) =
            if (!OSUtils.hasNougat()) // OS < 24 or OS < 7.0
                context?.createConfigurationContext(Configuration())
            else
                context
    }

   constructor(context: Context?) : super(getBrilliantContext(context))
   constructor(context: Context?, attrs: AttributeSet?) : super(getBrilliantContext(context), attrs)
   constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(getBrilliantContext(context), attrs, defStyleAttr)
}
Drucilladrucy answered 1/10, 2019 at 17:56 Comment(2)
@sumit-shukla posting the code snippet that helped us resolve the issue. But make sure you do this first https://mcmap.net/q/57813/-webview-resetting-uimode-and-breaking-dark-themeDrucilladrucy
Be careful, this workaround breaks drop down fields and selecting text in the WebView.Campania
T
2

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

implementation 'androidx.appcompat:appcompat:1.2.0-alpha02'

which will fix WebView issues on Lollipop.

Thar answered 30/1, 2020 at 13:13 Comment(2)
it fixes the issue but since it is alpha version it may cause other bugs. if you have tried it, haven't you faced any other issue in your app?Show
@Show No, I haven't experienced any glitches. Alpha versions are always a bit risky, of course. For example, a bug for alpha02 is mentioned here: issuetracker.google.com/issues/141351441#comment48Thar
O
2

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.

Oneill answered 30/1, 2020 at 20:2 Comment(0)
T
1

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

Thorstein answered 16/11, 2019 at 8:21 Comment(0)
P
1

In my case a problem was in method

fun getUserId(): Int = userId

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

private var userId: Int = 0

to

var userId: Int = 0
    private set

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.

Pike answered 11/3, 2020 at 15:56 Comment(0)
V
1

Instead of changing version of any dependency, I implemented programmatically generation of webView as a solution.

The problem actually looks like starting with reading layout from xml file. So if programmatic way is applicable solution for you please check bellow sample.

private WebView generateWebView(){
    WebView wv = new WebView(YourContext);
    wv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    wv.setFitsSystemWindows(false); // your preferences
    wv.setVerticalScrollBarEnabled(false); // your preferences
    wv.setPadding(15,15,15,15); // your preferences
    return wv;
}

Than just add into your parent view:

LinearLayout scrollContainer = findViewById(R.id.scrollContainer);
scrollContainer.addView(generateWebView());
Vardar answered 6/6, 2020 at 21:43 Comment(0)
V
1

Solution for November 2022

This issue is solve with do change on

implementation 'androidx.appcompact:appcompact:1.2.0-alpha02'

Special Note

Make sure you are not using getUserId() in your entire project. Change or replace this function.

Please do uninstall app and then check it. It's work for me.

Vaquero answered 21/11, 2022 at 7:3 Comment(0)
E
-4

Check version. Androidx appcompat and Google material version keep same always.

implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation "androidx.cardview:cardview:1.0.0"

enter image description here

Esposito answered 5/2, 2020 at 18:59 Comment(1)
You should paste the example code directly in your answer in case the image you linked becomes unavailable later.Upset

© 2022 - 2024 — McMap. All rights reserved.