My Android Java app shows some HTML content, but not on the initial screen. To make initial app loading faster, the WebView is initialized dynamically when needed:
m_TheEntry = new WebView(Ctxt); //Ctxt is an Activity m_TheEntry.setTag("Entry"); m_TheEntry.setWebViewClient(new EntryClient()); if(BuildConfig.DEBUG) m_TheEntry.setWebChromeClient(LoggingChromeClient.The); m_TheEntry.setLayoutParams(...); m_TheEntry.getSettings().setJavaScriptEnabled(true); m_TheEntry.addJavascriptInterface(m_JSHelper = new EntryJSHelper(), "host"); m_TheEntry.getSettings().setBuiltInZoomControls(true); //m_EntryFrame is a layout object m_EntryFrame.addView(m_TheEntry);
Most of the time, it works as expected. But I'm getting crash reports that the WebView construction throws various exceptions on user devices. The recent ones report that android.content.res.Resources$NotFoundException: Resource ID #0x40c0008, but some older ones are android.webkit.WebViewFactory$MissingWebViewPackageException: Failed to load WebView provider: No WebView installed .
How's that even possible? WebView is an SDK class, shouldn't it be present on all flavors of Android? And how does one sensibly fall back from that?
Yes as mentioned in the comment. Android WebView is part of the SDK but its actual WebView engine is a seperate component usually Android system WebView or chrome which can be update/enable from the playStore. There are the some cases like user might have disable the webView, OEM ROM has broken webView e.t.c in all these scenarios it will throw an exception. So its not your code issue but this is how webView design works.
I would suggest you to add try catch block for preventing this crash and show different UI when its not available or handle some error scenarios based on your requirement.
For instance:-
WebView webView;
try {
webView = new WebView(context);
} catch (Throwable error) {
Log.e("WebView", "WebView not available", error);
yourFallbackUI();
return;
}
Hussain Shabbir