科尔多瓦插件将无法工作
cordova plugin won't work
我已经安装了 Cordova 插件。一切顺利,直到我尝试使用该插件。
我只需要插件为 Android 工作。
这是我正在尝试使用的插件:
http://plugins.cordova.io/#/package/net.yoik.cordova.plugins.screenorientation
我确实看到插件已安装到我的目录中(插件 --> net.yoik.cordova.plugins.screenorientation)。
我的javascript代码:
window.onload = function () {
// access current orientation
console.log('Orientation is ' + screen.orientation);
screen.lockOrientation('portrait');
// access current orientation
console.log('Orientation is ' + screen.orientation);
};
console.log显示'undefined'。
我的 config.xml 文件:
<?xml version='1.0' encoding='utf-8'?>
<widget id="com.example.hello"
version="0.0.1"
xmlns="http://www.w3.org/ns/widgets"
xmlns:gap = "http://phonegap.com/ns/1.0"
xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>Shoot Out</name>
<author email="dev@cordova.apache.org" href="http://cordova.io">
Apache Cordova Team
</author>
<preference name="permissions" value="none"/>
<!-- Customize your app and platform with the preference element. -->
<preference name="orientation" value="default" />
<preference name="target-device" value="universal" />
<preference name="fullscreen" value="false" />
<preference name="webviewbounce" value="false" />
<preference name="prerendered-icon" value="true" />
<preference name="stay-in-webview" value="true" />
<preference name="ios-statusbarstyle" value="black-opaque" />
<preference name="detect-data-types" value="true" />
<preference name="exit-on-suspend" value="false" />
<preference name="show-splash-screen-spinner" value="true" />
<preference name="auto-hide-splash-screen" value="true" />
<preference name="disable-cursor" value="false" />
<preference name="android-minSdkVersion" value="7" />
<preference name="android-installLocation" value="auto" />
<!-- Plugins -->
<gap:plugin name="net.yoik.cordova.plugins.screenorientation" version="1.3.1" />
<access origin="*" />
我的索引文件:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="format-detection" content="telephone=no"/>
<meta name="msapplication-tap-highlight" content="no"/>
<!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
<!--<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=4, maximum-scale=1, minimum-scale=1, height=device-height, target-densitydpi=device-dpi" />-->
<meta name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, target-densitydpi=low-dpi, user-scalable=0"/>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<!-- FIREBASE CONNECTION -->
<script src="https://cdn.firebase.com/js/client/2.0.4/firebase.js"></script>
<script src="js/getCurrentGames.js"></script>
<script src="js/main.js"></script>
<title>Groot Project</title>
</head>
<body>
<div id="all"></div>
</body>
</html>
我已经在我的电脑和 phone (android) 上试过了,都出现了同样的错误。有什么想法吗?
不要使用 onload,而是使用 cordova/phonegap deviceready 事件,在设备准备好之前你不能使用插件
document.addEventListener("deviceready", function () {
// access current orientation
console.log('Orientation is ' + screen.orientation);
screen.lockOrientation('portrait');
// access current orientation
console.log('Orientation is ' + screen.orientation);
}, false);
我通过添加以下内容修复了它:
<script type="text/javascript" src="cordova.js"></script>
给我的 index.html
我已经安装了 Cordova 插件。一切顺利,直到我尝试使用该插件。 我只需要插件为 Android 工作。 这是我正在尝试使用的插件:
http://plugins.cordova.io/#/package/net.yoik.cordova.plugins.screenorientation
我确实看到插件已安装到我的目录中(插件 --> net.yoik.cordova.plugins.screenorientation)。
我的javascript代码:
window.onload = function () {
// access current orientation
console.log('Orientation is ' + screen.orientation);
screen.lockOrientation('portrait');
// access current orientation
console.log('Orientation is ' + screen.orientation);
};
console.log显示'undefined'。
我的 config.xml 文件:
<?xml version='1.0' encoding='utf-8'?>
<widget id="com.example.hello"
version="0.0.1"
xmlns="http://www.w3.org/ns/widgets"
xmlns:gap = "http://phonegap.com/ns/1.0"
xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>Shoot Out</name>
<author email="dev@cordova.apache.org" href="http://cordova.io">
Apache Cordova Team
</author>
<preference name="permissions" value="none"/>
<!-- Customize your app and platform with the preference element. -->
<preference name="orientation" value="default" />
<preference name="target-device" value="universal" />
<preference name="fullscreen" value="false" />
<preference name="webviewbounce" value="false" />
<preference name="prerendered-icon" value="true" />
<preference name="stay-in-webview" value="true" />
<preference name="ios-statusbarstyle" value="black-opaque" />
<preference name="detect-data-types" value="true" />
<preference name="exit-on-suspend" value="false" />
<preference name="show-splash-screen-spinner" value="true" />
<preference name="auto-hide-splash-screen" value="true" />
<preference name="disable-cursor" value="false" />
<preference name="android-minSdkVersion" value="7" />
<preference name="android-installLocation" value="auto" />
<!-- Plugins -->
<gap:plugin name="net.yoik.cordova.plugins.screenorientation" version="1.3.1" />
<access origin="*" />
我的索引文件:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="format-detection" content="telephone=no"/>
<meta name="msapplication-tap-highlight" content="no"/>
<!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
<!--<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=4, maximum-scale=1, minimum-scale=1, height=device-height, target-densitydpi=device-dpi" />-->
<meta name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, target-densitydpi=low-dpi, user-scalable=0"/>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<!-- FIREBASE CONNECTION -->
<script src="https://cdn.firebase.com/js/client/2.0.4/firebase.js"></script>
<script src="js/getCurrentGames.js"></script>
<script src="js/main.js"></script>
<title>Groot Project</title>
</head>
<body>
<div id="all"></div>
</body>
</html>
我已经在我的电脑和 phone (android) 上试过了,都出现了同样的错误。有什么想法吗?
不要使用 onload,而是使用 cordova/phonegap deviceready 事件,在设备准备好之前你不能使用插件
document.addEventListener("deviceready", function () {
// access current orientation
console.log('Orientation is ' + screen.orientation);
screen.lockOrientation('portrait');
// access current orientation
console.log('Orientation is ' + screen.orientation);
}, false);
我通过添加以下内容修复了它:
<script type="text/javascript" src="cordova.js"></script>
给我的 index.html