WebView code generating Uncaught TypeError and Uncaught ReferenceError errors on Android 4.4.2 (API 19) emulator

前端 未结 3 1777
误落风尘
误落风尘 2021-02-06 18:58

I\'m having a problem with my code when running on a Android 4.4.2 KitKat (API 19) emulator...

When I emulate my project on a Android 4.3 (API 18) emulator, it works nor

3条回答
  •  终归单人心
    2021-02-06 19:23

    I got this problem too.

    Here is my solution:

    1. change your base URL from "http://test" to "http://test/"
    2. change this code webView.loadUrl(..) to webView.evaluateJavascript(..);. Especially for code where we want to load innerHTML. Don't forget to add anotation @TargetApi(Build.VERSION_CODES.KITKAT) because it's only for Android 4.4 and above
    3. never put webView.loadWithBaseUrl(..) in the same process with webView.loadUrl(..). Because If webView.loadUrl(..) was loaded before webView.loadWithBaseUrl(..) finished, it will raise error as OP stated above.

    For number 3, your codes is already conform with this (because in your codes, webView.loadUrl(..) execution was already separated with webView.loadWithBaseUrl(..) by using OnClick event. So, don't pay attention to it.

    But if your apps need to load them at one event, consider to separate them by using this code: `

    private void initiateWebView(){
    
        webViewEquationDisplay.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
    
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
                    loadUrlKitKat(equationSymbolFinal+equationToBeDisplayedFinal);
                }
                else{
                    webViewEquationDisplay.loadUrl("javascript:document.getElementById('math').innerHTML='`"+equationToBeDisplayedFinal+"`';");
                }
    
                webViewEquationDisplay.loadUrl("javascript:MathJax.Hub.Queue(['Typeset',MathJax.Hub]);");
            }
        });
    
        final String mathJaxOfflineUrl = "file:///android_asset/MathJax/MathJax.js";            
        webViewEquationDisplay.loadDataWithBaseURL("http://bar/", ""
                +"","text/html","utf-8","");
    }
    
    
    @TargetApi(Build.VERSION_CODES.KITKAT)
    private void loadUrlKitKat(String param){
        webViewEquationDisplay.evaluateJavascript("javascript:document.getElementById('math').innerHTML='`"+param+"`';",null);
    }
    

    `

    good luck

提交回复
热议问题