cordova插件不起作用

时间:2015-01-15 10:08:59

标签: javascript cordova phonegap-plugins cordova-plugins

我已经安装了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>

我已经在我的电脑和手机(android)上尝试过,两者都有同样的错误。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

我通过添加以下内容修复了它:

<script type="text/javascript" src="cordova.js"></script>

到我的index.html

答案 1 :(得分:0)

使用cordova / phonegap deviceready事件,而不是使用onload,在设备准备好之前不能使用插件

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);