使用此代碼加載url.Url
在android &桌面瀏覽器中工作正常。 我已經編寫Android代碼來顯示android中的確認框。它是由在控制檯給錯誤的工作在華爲設備顯示在Nexus和三星的設備很好,但沒有確認框在android webview中的JS確認框不工作
"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);
}
}
請重新說明這個問題,因爲我不明白什麼工作正常,哪些不是。 –
使用JS的確認框只能在Nexus和三星設備上工作,而其在Huwei設備中沒有顯示任何確認框,並且即時獲取日誌爲未捕獲類型錯誤:無法調用方法'null'的querySelector',源:http:// abc/build /js/frontend-abc.js(16683) – Erum