1

I am trying to load a local HTML file in my Android application's webview. The HTML file references a local copy of jquerymobile.js. My problem is when the application is launched, the following error is thrown:

TypeError 'undefined' is not an object (evaluating $ event)

This happens even without any code written that uses the jquery lib.This error does not happen when I load the jquery library from their CDN.

Any ideas as to what I am doing wrong? Thanks for your time.

Added code for more clarity:

This is where the WebView loads the url

try {
        //this reads the jquerymobile as a string
        jmobile = readFileAsString();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
  wview1.setWebChromeClient(new WebChromeClient() {
        @Override
        public boolean onJsAlert(WebView view, String url, String message,           JsResult result) {
                    //Required functionality here
                    return super.onJsAlert(view, url, message, result);
           }   
  });
 wview1.setWebViewClient(new WebViewClient() {
        @Override
       public void onPageFinished(WebView view, String url) {
            super.onPageFinished(wview1, url);
              wview1.loadUrl("javascript:"+jmobile);
            // do your stuff here
        }
    });
    wview1.loadUrl("file:///android_asset/html/index.html");        

The following works

<!DOCTYPE html> 
<html> 
    <head> 
<title>My Page</title> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.css" />
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script>

But if I change the script src to a local copy of jquery.mobile-1.1.1.min.js, I get the type error

4

2 回答 2

2

当您引用本地文件时,请按以下顺序放置这些引用:

  1. 移动端 CSS
  2. jQuery核心的JS
  3. jQuery 移动端的 JS。

希望能帮助到你。

于 2012-09-11T09:28:18.763 回答
0

我理解您的问题如下:您在本地有一个 HTML 文件和 JS 文件。现在你需要加载 HTML 和 JS。但是失败了。

你能告诉我如何加载 HTML 文件并将 jquerymobile.js 注入其中。

我通常这样做:

WebView.setWebViewClient(new myWebViewClient ());

clsss myWebViewClient extend WebViewClient{
     @Override
     OnPageFinished(View view, String Url){
          view.loadUrl("javascript:"+String in jquerymobile.js);
     }
}

WebView.loadUrl("test.html");

这会对你有帮助吗?

于 2012-09-03T16:18:25.963 回答