1

In my Activity I added Webview (nothing abnormal):

<WebView
        tools:context=".Activities.WebViewActivity"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView"/>

And in my WebViewActivity I declarated it:

webview = findViewById(R.id.webView);
webview.setWebViewClient(new AppWebViewClients());
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebChromeClient(new WebChromeClient());
webview.getSettings().setDefaultTextEncodingName("utf-8");

The problem is my app crashing on some android devices (saw it in Crashlytics)... I reproduced this situation on emulator and get this Log (first part):

java.lang.RuntimeException: Unable to start activity ComponentInfo{jamhome.ru.lktsj/ru.domyland.superdom.Activities.WebViewActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class android.webkit.WebView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        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)

I found good solution - create Class and use it instead basic Webview:

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());
    }
}

Okay, no more crashes, good, BUT: I loaded the page which contains Select tag in html, and it didn't work if clicked on! Basically, android must open system dialog with Select's variables, but it doesn't...

Any ideas..?

2 Answers2

0

Simply do-:

Change

<WebView
        tools:context=".Activities.WebViewActivity"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView"/>

to -:

<yourpackgename.LollipopFixedWebView
        tools:context=".Activities.WebViewActivity"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView"/>

and change in java file also

LollipopFixedWebView webview=findViewById(R.id.webview);
Shivam Oberoi
  • 1,447
  • 1
  • 9
  • 18
  • 1
    No, this solution blocks Select tag in html! – Progressier Sep 04 '19 at 07:58
  • I mean that if I use this code app will not crash, but in my LollipopFixedWebView I can't use and draw custom dialog instead native? Maybe there is some code I need to write to LollipopFixedWebView to fix this issue? – Progressier Sep 04 '19 at 08:13
0

check out my anwer in HERE

in short: use fixed Context only on OS version on which default Context causes InflateException

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;
}
snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • Good, but I suppose that issue with html's – Progressier Sep 04 '19 at 08:34
  • @ snachmsm No, it doesn't... So, I've got a new issue. If I press – Progressier Sep 04 '19 at 09:16
  • @Progressier Any luck? I also faced this issue and used this solution https://stackoverflow.com/a/58684229/1089841 – Girish Nov 05 '19 at 13:12
  • Hello again, and yes, solution has been founded, but I don't know how did I do it... I remember, that in some cases it was Android failure, when some devices/systems doesn't have stable WebView component or Google Play Services – Progressier Nov 06 '19 at 07:46