Android application closes without errors - WebView Javascript

I have a webview with an HTML webpage downloaded from the server, and I fill in the dynamic data and add lines of information to this webpage. I used the following code on this webpage to enable javascript:

class JSClass { Context jsContext; JSClass(Context c){ jsContext = c; } public void getHTMLContent(String info) { Log.i(TAG, "Info: "+info); } } WebView webview = (WebView) findViewById(R.id.webView); webview.loadDataWithBaseURL("", htmlcontent, "text/html", "utf-8", ""); webview.getSettings().setJavaScriptEnabled(true); webview.setWebChromeClient(new WebChromeClient()); webview.addJavascriptInterface(new JSClass(this), "Android"); webview.loadUrl("javascript:"+ "var rows = document.getElementsByClassName('info');"+ "if(rows !== null){"+ "for(var i=0;i<rows.length;i++){"+ "rows[i].onclick = function(){"+ "var workId = 0;"+ "workId = parseInt(this.cells[0].innerHTML);"+ "window.Android.getHTMLContent(workId);"+ "}}}" ); 

when I click on any line, I intend to download images from the server depending on the data in the line. It works fine on the first click, but the second time I click, the application closes without any errors. I do not understand the reason. Please help. thanks in advance

0
source share
2 answers

I think you need to use loadDataWithBaseURL again instead of just loadUrl .

See this link. There is a section explaining loadData that states:

Note that a similar JavaScript source policy means that a script running on a page loaded using this method will not be able to access content loaded using any other scheme than β€œdata”, including β€œhttp (s)”. To avoid this limitation, use loadDataWithBaseURL () with the appropriate base URL.

0
source

FINALLY!! fixed it. The adapter kept previously downloaded images, as well as images downloaded during the second click, and I do not understand why the application was closed without giving any memory leaks. In any case, it now works fine after use

 adapter.clear(); 

or reinitializing the adapter to zero after loading all thumb images (I don't know if this is very efficient). After cleaning the adapter, use

 adapter.notifyDataSetChanged(); 
0
source

Source: https://habr.com/ru/post/1487208/


All Articles