android - Phonegap 在 Android 模拟器中不工作

标签 android cordova

我遵循了基本 Phonegap 教程(Eclipse、Android SDK、ADT Phonegap)中的所有步骤,我在 assets/www 文件夹中创建了一个 HMTL 页面,运行它,它显示了我的 hello world html .

第 2 步:我在我的 HTML 页面的 CDN 上添加了对 jQuery Mobile 的引用:它起作用了。

第 3 步:我创建了一个新的 html 页面,复制粘贴了下面的代码,这是来自 Phonegap 网站的一个示例,但它...什么也没做。甚至没有警报(我添加了一些警报以查看是否发生了什么,但即使是 onDeviceReady 事件也不会触发。

我有 Phonegap JAR,cordova-1.7.0.js在我的 assest/www目录,但可能缺少某些内容。

有人能帮帮我吗?

我还尝试了 Phonegap 站点的另一个示例(“设备属性”示例),但它仍然不起作用。

这是全新的 Eclipse 安装,我已将 Android 版本设置为 2.3.3,并且我使用的是 Phonegap 1.7.0。

================

编辑

我又试了一些,现在可以重现错误,但不知道为什么会这样。

所以我用 Phonegap 示例项目创建了一个新项目,它工作正常。

因此,我将该项目中的所有 Assets (1 个 html、2 个 js 和 1 个 css)复制到我的项目中,让应用程序以该 html(来 self 的 Activity 类)开始,它就可以工作了。

现在是有趣的部分(不是):我将起始页重置为我的“旧”index.html(这是 jQuery 移动版),然后单击示例 html 的链接,它不起作用。

所以示例 html 作为启动:它有效,示例 html 通过链接打开:无效。

当我加载其他不能用作起始页的 html 页面时,它们也可以正常工作,而不是通过起始页打开它们。

那么,我的 jQuery Mobile 驱动的索引页面是否可能导致问题? (我将复制粘贴下面的代码)。

EDIT2:当我使用非 jQuery Mobile 索引页面并链接到正常的 <A href> 时链接到示例 html,它也有效。所以这越来越多地暗示我认为 jQuery Mobile 妨碍了我......

链接代码是这样的:

<li><a href="index4.html" data-transition="none">phonegap example</a></li>

jQuery Mobile 主页:

<html>
<head>
    <title></title>
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />
    <link rel="stylesheet" href="http://www.verfrisser.net/kalender/mobile/verfrisser.css" />
    <script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="cordova-1.7.0.js"></script>
</head>
<body>
  <div data-role="page">

    <div data-role="header">
        <h1>De NerdNight kalender</h1>
        <a href="about.html" data-rel="dialog">About</a><a href="genereren.html" data-transition="pop">Genereren</a>
    </div><!-- /header -->

    <div data-role="content">
        <img id="verfrisserlogo" src="verfrisserlogo.png" />
        <ul data-role="listview" data-inset="true" data-filter="false">
            <li><a href="2011.html" data-transition="none">2011</a></li>
            <li><a href="2012.html" data-transition="none">2012</a></li>
            <li><a href="2013.html" data-transition="none">2013</a></li>
            <li><a href="testing.html" data-transition="none">testing</a></li>
            <li><a href="testing2.html" data-transition="none">testing2</a></li>
            <li><a href="testing3.html" data-transition="none">testing3</a></li>
            <li><a href="index4.html" data-transition="none">phonegap example</a></li>
        </ul>
    </div><!-- /content -->

    <div data-role="footer">
        <h6>(C) Verfrisser 1998 till now</h6>
    </div><!-- /footer -->
</div><!-- /page -->
</body>
</html>

================

示例 HTML(仅在页面中显示文本“对话框将报告网络状态”)

<!DOCTYPE html>
<html>  
<head>    
    <title>navigator.network.connection.type Example</title>    
    <script type="text/javascript" charset="utf-8" src="cordova-1.7.0.js"></script> 
 <script type="text/javascript" charset="utf-8">    
   // Wait for Cordova to load    //    
   document.addEventListener("deviceready", onDeviceReady, false);  
   // Cordova is loaded and it is now safe to make calls Cordova methods   
    alert ('stand alone');
   //    
   function onDeviceReady() {
    alert ('onDeviceReady'); 
   checkConnection();   
   }    
   function checkConnection() {        
   var networkState = navigator.network.connection.type;        
   var states = {};       
   states[Connection.UNKNOWN]  = 'Unknown connection';        
   states[Connection.ETHERNET] = 'Ethernet connection';        
   states[Connection.WIFI]     = 'WiFi connection';       
   states[Connection.CELL_2G]  = 'Cell 2G connection';   
   states[Connection.CELL_3G]  = 'Cell 3G connection';   
   states[Connection.CELL_4G]  = 'Cell 4G connection';   
   states[Connection.NONE]     = 'No network connection';
   alert('Connection type: ' + states[networkState]); 
   }  

</script> 
</head>  
<body>
    <p>A dialog box will report the network state.</p>  
</body>
</html>

最佳答案

使用默认行为,下一页将作为 DOM 元素加载,因此不会再次调用 javascript deviceready

尝试使用以下方法之一以正常方式运行它:

<li><a href="testing2.html" data-transition="none" rel="external">testing2</a></li>

或者

<li><a href="testing2.html" data-transition="none" data-ajax="false">testing2</a></li>

解释:

When a link is clicked, jQuery mobile will make sure the link is referencing a local URL, and if so, it'll prevent the link's default click behavior from occurring and request the referenced url via Ajax instead. When the page returns successfully, it will set the location.hash to the new page's relative url.

If the Ajax request is successful, the new page content is added to the DOM, all mobile widgets are auto-initialized, then the new page is animated into view with a page transition.

有关更多详细信息,请查看 doc

关于android - Phonegap 在 Android 模拟器中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10922381/

相关文章:

android - 使用带有游标的加载器

java - 借鉴其他应用问题 |开关变灰

支持库中的 Android CalendarView

javascript - 使用按钮垂直滚动 div

angularjs - 在客户端将多个音频文件合并为一个音频文件

javascript - 当存储容量不足时,IOS 会删除 WebSQL 数据库吗?

android - Play 商店中的广告部分为空

android - 如何将 inflatedView 添加到线性布局

android - IBM Worklight 插件连接未发生

windows - 在 Cordova 的 Windows 8 应用程序中的 LocalState 文件夹中显示 PDF 文件