2

我正在尝试在视图寻呼机中加载 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()");
        }
    }
}

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

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

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

testFunction() 是

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

请帮忙...

4

3 回答 3

5

我对此进行了修复

只需设置 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;
    }
于 2013-09-06T15:04:41.487 回答
1

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

网页浏览代码

    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();
        }
    }

网页代码

<!DOCTYPE html>
<html>
  <head>
   <script>
     var i=0;
     function myFunction()
     {

       JSInterface.showToast();
     }
   </script>
 </head>

 <body>

    <button onclick="myFunction()">Try it</button>

    <p>By clicking the button above, a function will be called. The function will alert a message.</p>

 </body>
</html>

编辑

function myFunction()
 {

   do here somthing
 }
于 2013-09-06T09:15:21.837 回答
0

在 4.3 及以下版本中,我的混合应用程序的每一次启动都Uncaught Reference Error: JavascriptInterfaceName is not defined在进行,所以我做到了这要感谢我WebChromeClient班上的@Abi:

@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);
}

它有效!非常感谢你!

于 2015-04-02T14:43:14.313 回答