简体   繁体   English

Phonegap无法在Android模拟器中运行

[英]Phonegap not working in Android emulator

I followed all the steps in the basic Phonegap tutorial (Eclipse, Android SDK, ADT Phonegap), I created an HMTL page in the assets/www folder, ran it, and it showed my hello world html . 我按照基本的Phonegap教程(Eclipse,Android SDK,ADT Phonegap)中的所有步骤,在assets / www文件夹中创建了一个HMTL页面,运行它,它显示了我的hello world html

Step 2: I added a reference to jQuery Mobile on a CDN in my HTML page: it worked. 第2步:我在HTML页面的CDN上添加了对jQuery Mobile的引用:它工作正常。

Step 3: I created a new html page, copy-pasted the code below, which is a sample from the Phonegap site, and it does... nothing. 第3步:我创建了一个新的html页面,复制粘贴下面的代码,这是来自Phonegap网站的一个示例,它确实......没有。 Not even an alert (I added some alerts to see if something happens, but even the onDeviceReady event doesn't fire. 甚至没有警报(我添加了一些警报,看看是否有事情发生,但即使是onDeviceReady事件也不会触发。

I have the Phonegap JAR in place, the cordova-1.7.0.js in my assest/www directory, but something is probably missing. 我有Phonegap JAR,我的assest/www目录中的cordova-1.7.0.js ,但可能有些东西丢失了。

Can someone help me out? 有人可以帮我吗?

I also tried another sample from the Phonegap site (the 'device properties' sample), but it still doesn't work. 我还尝试了Phonegap网站上的另一个样本('设备属性'样本),但它仍然无效。

It's a fresh Eclipse install, I've set the Android version to 2.3.3 and I'm using Phonegap 1.7.0. 这是一个全新的Eclipse安装,我将Android版本设置为2.3.3,我使用的是Phonegap 1.7.0。

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

EDIT 编辑

I tried some more, and I now can reproduce the error, but don't know why it happens. 我尝试了一些,现在我可以重现错误,但不知道为什么会发生。

So I created a new project with the Phonegap example project, it works. 所以我使用Phonegap示例项目创建了一个新项目,它可以工作。

So, I copied all the assets from that project (1 html, 2 js and 1 css) to my projects, let the app start with that html (from my activity class), and it works. 所以,我将该项目中的所有资产(1个html,2个js和1个css)复制到我的项目中,让应用程序从该html开始(从我的活动类开始),并且它可以工作。

And now for the fun part (not): I reset the startpage to my 'old' index.html (which is jQuery mobile), and then clicked on a link to the example html, it does NOT work. 现在为了有趣的部分(不是):我将起始页重置为我的'旧'index.html(这是jQuery mobile),然后点击示例html的链接,它不起作用。

So example html as startup: it works, example html opened via the link: does NOT work. 所以示例html作为启动:它工作,通过链接打开示例html:不起作用。

And when I loaded my other html pages which didn't work as start page, instead of opening them via the start page, they work too. 当我加载其他不起作为起始页面的html页面,而不是通过起始页面打开它们时,它们也起作用。

So, is it possible that my jQuery Mobile-powered index page causes trouble ? 那么,我的jQuery Mobile支持的索引页面是否可能导致问题呢? (I'll copy-paste the code below). (我将复制粘贴下面的代码)。

EDIT2: When I use a non-jQuery Mobile index page and I link with a normal <A href> link to the example html, it also works. EDIT2:当我使用非jQuery Mobile索引页面并且我链接到示例html的正常<A href>链接时,它也可以。 So that's hinting more and more to the fact that I think jQuery mobile is in my way... 所以我越来越暗示我认为jQuery mobile在我的方式......

The link code is this: 链接代码是这样的:

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

The jQuery Mobile home page: 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>

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

The sample HTML (which only shows the text 'A dialog box will report the network state' in the page) 示例HTML(仅显示文本'A对话框将在页面中报告网络状态')

<!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>

With default behavior the next page will be loaded as a DOM element so the javascript deviceready will not be called again. 使用默认行为时,下一页将作为DOM元素加载,因此不会再次调用javascript deviceready

Try with one of these ways to run it the normal way: 尝试使用以下方法之一以正常方式运行它:

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

Or 要么

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

Explanation: 说明:

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. 单击链接时,jQuery mobile将确保链接引用本地URL,如果是,则会阻止链接的默认单击行为发生,并通过Ajax请求引用的URL。 When the page returns successfully, it will set the location.hash to the new page's relative url. 当页面成功返回时,它会将location.hash设置为新页面的相对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. 如果Ajax请求成功,则将新页面内容添加到DOM,所有移动窗口小部件都自动初始化,然后将新页面设置为带有页面转换的视图。

For more detail info check the doc 有关更多详细信息,请查看文档

As far as no bug in your code. 至于你的代码中没有错误。 This above code structure is not well constructed. 上面的代码结构没有很好地构造。 You mentioned the phonegap and jquery mobile javascript in the head of first page and not in the second page. 你在第一页的头部而不是第二页中提到了phonegap和jquery移动javascript。 It does not have any jquery mobile javascript file in the head. 它的头部没有任何jquery移动javascript文件。

Facts from my knowledge : 据我所知的事实:

  • You can do Jquery transition from one jquery mobile page to another jquery mobile included page. 您可以将Jquery从一个jquery移动页面转换到另一个jquery移动包含页面。
  • Include the standard library files in the both header. 在两个标头中包含标准库文件。

This will do your job. 这将完成你的工作。

添加@Coder_sLaY提到的xml文件后,需要将加载jQuery Mobile JS文件(CDN URL)的服务器添加到cordova.xml中允许的服务器的白名单中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Phonegap无法在android模拟器上运行(找不到index.html) - Phonegap not working on android emulator(index.html not found) 自定义字体在Android模拟器上工作,但不在设备上。 (PhoneGap的) - Custom font working on android emulator but not on device. (Phonegap) 带sqlite的PhoneGap(3.4.0)android应用,在模拟器中工作正常,但在设备中工作不正常 - PhoneGap(3.4.0) android app with sqlite, its working fine in emulator but not in device PhoneGap数据库在模拟器上运行,但不在设备上运行 - PhoneGap database working on emulator, but not on device Phonegap模拟器错误-&#39;模拟android&#39;不是phonegap命令 - Phonegap Emulator Error - 'emulate android' is not a phonegap command PhoneGap App未在Android Emulator中运行 - PhoneGap App not running in Android Emulator 使用phonegap在android模拟器中调试javascript - debug javascript in android emulator with phonegap 使用PhoneGap的Ripple Emulator + android项目 - Ripple Emulator + android project with PhoneGap 代码适用于所有浏览器但不适用于模拟器“phonegap” - Code working on all browsers but not working on Emulator “phonegap” 在android phonegap应用程序语言(印度语印地语)中,字体在android模拟器中可用,但在实际设备中不起作用。 - In android phonegap application language (Indian language Hindi) font is worked in android emulator but it is not working in real device.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM