Enabled Javascript on WebView. Because without JavaScipt web page image scaling is not working properly. This is my code how I am loading url in WebView:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String url = "http://nuz.uz/ekonomika-i-finansy/18651-v-2017-godu-v-uzbekistane-prekratitsya-realizaciya-lampochek-ilicha.html";
final WebView webview = (WebView) findViewById(R.id.webview);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true);
}
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebChromeClient(new WebChromeClient() {
public void onConsoleMessage(String message, int lineNumber, String sourceID) {
Log.d("Tag", message + " -- From line "
+ lineNumber + " of "
+ sourceID);
}
});
webview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String loadingUrl) {
if (loadingUrl.equals(url)) return false;//open news link in webview
else {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(loadingUrl));
startActivity(browserIntent);
return true;
}
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error){
Log.d("Tag", "Error occured: "+error.toString());
}
});
webview.loadUrl(url);
}
}
When I am running app, it is opening web page. After loading all content of page, app is crashing. I tried to show error on console it is showing following error:
12-14 02:42:06.936 25760-25760/? D/Tag: Uncaught TypeError: Cannot call method 'init' of undefined -- From line 951 of http://nuz.uz/ekonomika-i-finansy/18651-v-2017-godu-v-uzbekistane-prekratitsya-realizaciya-lampochek-ilicha.html
I have checked url's content and found following which is causing app crash :
<script type="text/javascript">
jQuery(document).ready(function () {
App.init();
});
</script>
line 951: App.init();
How I can solve this problem without turning off JavaScript? How to prevent WebView from crashing app?