JS confirmation box in Android browser does not work

Using this code to download url.Url works fine in the android and desktop browser. I am writing Android code to show the "Verify" checkboxes in android. It works great on Nexus and Samsung devices, but doesn’t display the confirmation fields shown on the Huawei device, causing an error in the console

"Uncaught TypeError: cannot call 'querySelector' method from null", source: http: //abc/build/js/frontend-abc.js (16683)

 private class WebViewChromeClient extends WebChromeClient { @Override public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result) { new AlertDialog.Builder(context) .setTitle(getString(R.string.str_confirmation_title)) .setMessage(message) .setPositiveButton(getString(R.string.str_ok), new AlertDialog.OnClickListener() { public void onClick(DialogInterface dialog, int which) { result.confirm(); } }).setCancelable(false).create().show(); return true; } @Override public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) { new AlertDialog.Builder(context) .setTitle(getString(R.string.str_confirmation_title)) .setMessage(message) .setPositiveButton(getString(R.string.str_ok), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { result.confirm(); } }).setNegativeButton(getString(R.string.str_cancel), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { result.cancel(); } }).create().show(); return true; } @Override public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, final JsPromptResult result) { final LayoutInflater factory = LayoutInflater.from(context); final View v = factory.inflate(R.layout.layout_alertdialog, null); ((TextView) v.findViewById(R.id.tv_messagealert)).setText(message); showJSPromptAlert(v, result); return true; } } private void showJSPromptAlert(View v, final JsPromptResult result) { AlertDialog.Builder builder = new AlertDialog.Builder(context) .setTitle(getString(R.string.str_confirmation_title)) .setView(v) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { result.confirm(getString(R.string.str_ok)); } }) .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { result.cancel(); } }) .setOnCancelListener( new DialogInterface.OnCancelListener() { public void onCancel(DialogInterface dialog) { result.cancel(); dialog.cancel(); } }); alert11 = builder.create(); alert11.show(); } WebView wvContainer = (WebView) findViewById(R.id.wv_container); private void loadUrl(String url) { wvContainer.setInitialScale(1); wvContainer.setWebViewClient(new MyBrowser()); adjustWebViewSettings(); wvContainer.canGoBack(); adjustWebViewForLatestVersion(); wvContainer.setWebChromeClient(new WebViewChromeClient()); wvContainer.loadUrl(url); } private void adjustWebViewSettings() { wvContainer.getSettings().setJavaScriptEnabled(true); wvContainer.getSettings().setSupportZoom(true); wvContainer.getSettings().setAllowContentAccess(true); wvContainer.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); wvContainer.getSettings().setSupportMultipleWindows(true); wvContainer.getSettings().setDomStorageEnabled(true); wvContainer.getSettings().setAppCacheEnabled(true); wvContainer.getSettings().setUseWideViewPort(true); if (Build.VERSION.SDK_INT >= 21) { wvContainer.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); } } private void adjustWebViewForLatestVersion() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { wvContainer.getSettings().setAllowUniversalAccessFromFileURLs(true); wvContainer.getSettings().setAllowFileAccessFromFileURLs(true); wvContainer.getSettings().setAllowFileAccess(true); wvContainer.setLayerType(View.LAYER_TYPE_SOFTWARE, null); } } 
0
javascript jquery android android-webview android-alertdialog
Jan 9 '17 at 11:42 on
source share
1 answer

"Uncaught TypeError: Cannot call method 'querySelector' of null" basically means that the JS code in the webview is trying to access the querySelector object of what is interpreted as null.

Zero is primitive and has no properties. Thus, you probably have some programming error based on the existence of A for B, but A does not seem to exist (querySelector is a method in a document or elements).

What to do

Try to get the device error JS stack trace, and find out why the thing that SHOULD provide a Selector request (document or item) is null. Then solve this problem and see if everything is good ...

Possible reasons

  • The document is not fully loaded when the code calls methods on it
  • Device specific code corrupt
  • Murphy's Law ...
0
Jan 09 '17 at 12:12
source share



All Articles