E / Web控制台(8272):未捕获的ReferenceError:未定义functionName:1在View Pager中加载webviews时

我正在尝试在视图寻呼机中加载webviews。

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = null; v = inflater.inflate(R.layout.webview_layout, container, false); myWebView = (WebView)v.findViewById(R.id.webview1); WebSettings webSettings = myWebView.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setAppCacheEnabled(true); webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); myWebView.loadUrl("file:///android_asset/web/index.html"); myWebView.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { myWebView.loadUrl("javascript:testFunction()"); } } } 

加载页面后,在onPageFinished()调用javascript函数在以正常速度滚动时,网页正在加载并执行javascript。

但是在高速滚动时会发生以下exception。

 > 09-06 14:29:06.750: E/Web Console(8272): Uncaught ReferenceError: > testFunction is not defined:1 

testFunction()是

 function testFunction(){ console.log("TestFuntion"); } 

请帮忙…

我解决了这个问题

只需设置webChromeClient并捕获错误并重新加载页面……

  myWebView.setWebViewClient(new WebViewClient() { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { isLoading = true; } @Override public void onPageFinished(WebView view, String url) { myWebView.loadUrl("javascript:testFunction()"); } }); myWebView.setWebChromeClient(new WebChromeClient() { @Override public boolean onConsoleMessage(ConsoleMessage consoleMessage) { MessageLevel level = consoleMessage.messageLevel(); if(level.ordinal() == 3) { // Error ordinal if(loading) { myWebView.stopLoading(); myWebView.loadUrl(AppConstants.ARTICLE_PAGE_URL); } } } return false; } 

尝试这样的事情,并根据您的要求进行更改

webview代码

  web.getSettings().setJavaScriptEnabled(true); JavaScriptInterface JSInterface = new JavaScriptInterface(this); web.addJavascriptInterface(JSInterface, "JSInterface"); setContentView(web); web.loadUrl("file:///android_asset/demo.html"); 

javascript界面

 public class JavaScriptInterface { Context mContext; /** Instantiate the interface and set the context */ JavaScriptInterface(Context c) { mContext = c; } public void showToast() { Toast.makeText(mContext, "Button Clicked", Toast.LENGTH_SHORT).show(); } } 

网页代码

        

By clicking the button above, a function will be called. The function will alert a message.

编辑

 function myFunction() { do here somthing } 

我得到了Uncaught Reference Error: JavascriptInterfaceName is not defined我的混合应用程序的每次第二次启动Uncaught Reference Error: JavascriptInterfaceName is not defined在4.3及以下版本中Uncaught Reference Error: JavascriptInterfaceName is not defined ,所以我这样做归功于我的WebChromeClient类中的WebChromeClient

 @Override @SuppressLint({"AddJavascriptInterface", "InflateParams"}) public boolean onConsoleMessage(@NonNull ConsoleMessage consoleMessage) { if("Uncaught ReferenceError: JavascriptInterfaceName is not defined".equals(consoleMessage.message())) { webView.addJavascriptInterface(new WebAppInterface(), "JavascriptInterfaceName "); webView.reload(); } return super.onConsoleMessage(consoleMessage); } 

它的工作原理! 非常感谢你!