简体   繁体   中英

Error inflating class android.webkit.WebView Crash on Lollipop when opening PDF

In my app I encountered said Error during auto-tests. I'm attempting to show a PDF from an URL in a WebView which causes this crash on devices running Android 5 Lollipop. The full crash log references a String resource in WebView not being found, which I couldn't really gain much from either so far.

EDIT: I am aware this is a known problem, but so far no solution has fixed the crash, only prevented Android 5 devices from reaching that state and attempting to show the pdf.

Crashlog

android.view.InflateException: Binary XML file line #6: 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 de.buhl.authenticator.LicenseFragment.onCreateView(LicenseFragment.java:27)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
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:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
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 de.buhl.authenticator.LicenseFragment.onCreateView(LicenseFragment.java:27) 
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600) 
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881) 
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238) 
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303) 
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439) 
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079) 
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869) 
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824) 
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727) 
at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150) 
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:5221) 
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:899) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2040002
at android.content.res.Resources.getText(Resources.java:274)
at android.content.res.Resources.getString(Resources.java:360)
at com.android.org.chromium.content.browser.ContentViewCore.setContainerView(ContentViewCore.java:702)
at com.android.org.chromium.content.browser.ContentViewCore.initialize(ContentViewCore.java:608)
at com.android.org.chromium.android_webview.AwContents.createAndInitializeContentViewCore(AwContents.java:619)
at com.android.org.chromium.android_webview.AwContents.setNewAwContents(AwContents.java:758)
at com.android.org.chromium.android_webview.AwContents.<init>(AwContents.java:608)
at com.android.org.chromium.android_webview.AwContents.<init>(AwContents.java:546)
at com.android.webview.chromium.WebViewChromium.initForReal(WebViewChromium.java:312)
at com.android.webview.chromium.WebViewChromium.access$100(WebViewChromium.java:97)
at com.android.webview.chromium.WebViewChromium$1.run(WebViewChromium.java:264)
at com.android.webview.chromium.WebViewChromium$WebViewChromiumRunQueue.drainQueue(WebViewChromium.java:124)
at com.android.webview.chromium.WebViewChromium$WebViewChromiumRunQueue$1.run(WebViewChromium.java:111)
at com.android.org.chromium.base.ThreadUtils.runOnUiThread(ThreadUtils.java:144)
at com.android.webview.chromium.WebViewChromium$WebViewChromiumRunQueue.addTask(WebViewChromium.java:108)
at com.android.webview.chromium.WebViewChromium.init(WebViewChromium.java:261)
at android.webkit.WebView.<init>(WebView.java:548)
at android.webkit.WebView.<init>(WebView.java:483)

onCreateView

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_license, container, false);
webView = view.findViewById(R.id.web_view_license);
webView.loadUrl(getResources().getString(R.string.url_licenses));
return view;
}

layout file

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<WebView
android:id="@+id/web_view_license"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>

It seems that this problem is known when used Android Lollipop.

Did you try these solutions? android.view.InflateException Error inflating class android.webkit.WebView

update your appcompat version to 1.2.0-alpha03.

 implementation "androidx.appcompat:appcompat:1.2.0-alpha03"

A bit late, but here is a workaround that should work:

(add to your Activity class that contains the WebView)

    @Override
    public void applyOverrideConfiguration(Configuration configuration) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M
                && getResources().getConfiguration().uiMode == WikipediaApp.getInstance().getResources().getConfiguration().uiMode) {
            return;
        }
        super.applyOverrideConfiguration(configuration);
    }

Just add this new dependencies:

implementation 'androidx.appcompat:appcompat:1.2.0-beta01'
implementation 'androidx.appcompat:appcompat-resources:1.2.0-beta01'

The documentation says:

Fixed an issue where androidx.appcompat:appcompat:1.1.0 crashes webview when webview is long pressed (b/141351441)

You made a mistake when writing.

`WebView webView = view.findViewById(R.id.web_view_license);

It should be WebView = (WebView)view.findViewById(R.id.web_view_license);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM