Javascript addEventListener don';它似乎在工作
我是一个新的phonegap用户,我正在试用我在youtube上找到的指南针教程 除了指南针不动之外,一切似乎都运转良好 嗯,我想我的addEventListener不知怎么地不起作用了 我也不知道为什么 有什么帮助吗 我的项目文件结构 链接到我的项目文件以便于查看 我的代码在下面 MainActivity.javaJavascript addEventListener don';它似乎在工作,javascript,android,webview,cordova,Javascript,Android,Webview,Cordova,我是一个新的phonegap用户,我正在试用我在youtube上找到的指南针教程 除了指南针不动之外,一切似乎都运转良好 嗯,我想我的addEventListener不知怎么地不起作用了 我也不知道为什么 有什么帮助吗 我的项目文件结构 链接到我的项目文件以便于查看 我的代码在下面 MainActivity.java package com.example.compass; import android.app.Activity; import android.os.Bundle; imp
package com.example.compass;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setGeolocationEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("file:///android_asset/www/index.html");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Index.html
<!DOCTYPE html>
<html>
<title> </title>
<head>
<style>
#compass {
position: absolute;
top: 0px;
left: 0px;
z-index: 1;
}
#needle {
position: absolute;
top: 8px;
left: 98px;
z-index: 10;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<script type="text/javascript" charset="utf-8" src="js/cordova.js"></script>
<script src="jqRotate/jqRotate.js"></script>
</head>
<body onload="onLoad()">
<img src="http://nathanhedglin.us/images/compass.png" id="compass">
<img src="http://nathanhedglin.us/images/needle.png" id="needle">
</body>
</html>
<script>
function onDeviceReady() {
alert("Starting!");
startCompass(); //runs the startCompass function
}
function onLoad() {
alert("Loading!!");
document.addEventListener("deviceready", onDeviceReady, false); //creates a listener that waits for the deviceready event and then fires onDevice ready function
}
function startCompass() {
var options = {
frequency : 50 //sets the compass heading to be updated every 50 milliseconds
};
navigator.compass.watchHeading(onSuccess, onError, options); //gets the compass heading from device and passes heading to onSuccess function
alert("Compass starting!!");
}
function onSuccess(heading) {//device s magnetic heading is passed to heading variable
$("#needle").rotate(-heading.magneticHeading); //jquery function that gets the #needle id and rotates it minus the current heading
}
function onError(compassError) { //if there is an error the error will be passed to onError in the compassError variable
alert('Compass error: ' + compassError.code); //displays an alert with the error code
}
</script>
#罗盘{
位置:绝对位置;
顶部:0px;
左:0px;
z指数:1;
}
#针头{
位置:绝对位置;
顶部:8px;
左:98px;
z指数:10;
}
函数ondevicerady(){
警报(“启动!”);
startCompass();//运行startCompass函数
}
函数onLoad(){
警报(“加载!!”;
document.addEventListener(“deviceready”,onDeviceReady,false);//创建一个侦听器,等待deviceready事件,然后启动onDeviceReady函数
}
函数startCompass(){
变量选项={
频率:50//设置每50毫秒更新一次罗盘航向
};
navigator.compass.watchHeading(onSuccess,onError,options);//从设备获取指南针航向,并将航向传递给onSuccess函数
警报(“指南针启动!!”;
}
函数onSuccess(heading){//将设备的磁航向传递给航向变量
$(“#针”).rotate(-heading.magnizeReading);//jquery函数,该函数获取#针id并将其旋转减去当前标题
}
函数onError(compassError){//如果有错误,错误将在compassError变量中传递给onError
警报('指南针错误:'+compassError.code);//显示带有错误代码的警报
}
您不能在onLoad中使用deviceready,因为deviceready在onLoad之前已启动。我个人建议不要使用PhoneGap的onLoad
你的头应该是这样的:
// All of your scripts loaded above
<script>
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
// This is where you should initialize your application
}
</script>
//上面加载的所有脚本
文件。添加的监听器(“deviceready”,OnDeviceraddy,false);
函数ondevicerady(){
//这是您应该初始化应用程序的地方
}
DeviceRady应该是您添加的第一件事情,然后在启动DeviceRady之后添加任何其他事件侦听器