JS confirm box in android webview not working

匿名 (未验证) 提交于 2019-12-03 01:10:02

问题:

Using this code to load url.Url is working fine in android & desktop browser. I have write Android code to show Confirm boxes in android. It's working fine in Nexus and Samsung device but no Confirm boxes shown in Huawei device by giving error in console

"Uncaught TypeError: Cannot call method 'querySelector' of 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);         }     }

回答1:

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

Null is a primitive and doesn't have properties. So you probably have some programming error that relies on the existence of A for B to be populated but A doesn't seem to exist (querySelector is a method in document or elements).

What to do

Try to obtain a JS stack trace of the device errors an figure out why the the thing that SHOULD provide querySelector (document or an element) is null. Then solve that issue, and see if all shines well...

Possible causes

  • Document is not fully loaded when code is calling methods on it
  • Device specific code gone bad
  • Murphy's Law...


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!