PhoneGap Build:在Android上插件不工作(获取“未定义”错误) [英] PhoneGap Build: Plugins not working (getting "undefined" errors) on Android
问题描述
我在这里有一个非常讨厌的问题。每次,我尝试在我的项目中使用文件和文件传输插件,我得到未捕获的类型错误:无法读取属性'dataDirectory'未定义错误,试图获取数据目录 store = cordova.file.dataDirectory;
。现在,我一直在试图解决我的问题几个小时,但我找不到任何帮助。我也使用条码扫描器插件在这个项目,它的工作方式像一个魅力。我使用PhoneGap 3.6.3和jQuery Mobile 1.4.4。因为我是新的PhoneGap,它很可能是我缺少一些重要的东西...
I've got a really nasty problem here. Every time, I try to use the file and file-transfer plugins in my project, I get the Uncaught TypeError: Cannot read property 'dataDirectory' of undefined error while trying to get the data directory with store = cordova.file.dataDirectory;
. Now, I've been trying to solve my problem for hours, but I couldn't find any help. I'm also using the Barcode Scanner plugin in this project an it's working like a charm. I am using PhoneGap 3.6.3 and jQuery Mobile 1.4.4. Since i'm new to PhoneGap, it's likely that I'm missing something important...
我已经在我的config.xml包括这样的插件: / p>
I've included the plugins in my config.xml like this:
<gap:plugin name="org.apache.cordova.file" version="1.3.1" />
<gap:plugin name="org.apache.cordova.file-transfer" version="0.4.6" />
也许这可能是一个提示:Windows Phone的PhoneGap Build日志确认插件正在添加。但是它似乎并没有添加到Android版本,因为我不能在生成日志中找到他们的任何引用。
Maybe this could be a hint: The PhoneGap Build log for Windows Phone is confirming that the plugins are being added. But it appears that they're not being added to the Android build, since I can't find any reference of them in the build log.
Windows Phone日志:
Windows Phone Log:
添加www\plugins\org.apache.cordova.file-transfer\www\FileTransfer.js
Adding www\plugins\org.apache.cordova.file-transfer\www\FileTransfer.js
添加www\plugins\org.apache.cordova.file-transfer\www\FileTransferError.js
Adding www\plugins\org.apache.cordova.file-transfer\www\FileTransferError.js
添加www\plugins\org.apache.cordova.file\www\File.js
Adding www\plugins\org.apache.cordova.file\www\File.js
这是我的一部分index.js,不包括条形码扫描器功能。
This is a part of my index.js, excluding the Barcode Scanner functionality.
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
document.getElementById('download').addEventListener('click', this.downloadFile, false);
document.getElementById('scan').addEventListener('click', this.scan, false);
document.getElementById('encode').addEventListener('click', this.encode, false);
},
onDeviceReady: function() {
app.receivedEvent('deviceready');
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
},
downloadFile: function(){
//The directory to store data
var store;
//Used for status updates
var $status;
//URL of our asset
var assetURL = "https://www.dropbox.com/s/d4s8mnkfwdqylns/test.txt?dl=0";
//File name of our important data file we didn't ship with the app
var fileName = "test.txt";
document.addEventListener("deviceready", init, false);
function init() {
$status = document.querySelector("#fileStatus");
$status.innerHTML = "Checking for file";
store = cordova.file.dataDirectory;
//Check for the file.
window.resolveLocalFileSystemURL(store + fileName, appStart, downloadAsset);
}
function downloadAsset() {
var fileTransfer = new FileTransfer();
console.log("About to start transfer");
fileTransfer.download(assetURL, store + fileName,
function(entry) {
console.log("Success!");
appStart();
},
function(err) {
console.log("Error");
console.dir(err);
});
}
function appStart() {
$status.innerHTML = "Datei aktuell";
}
},
// [...Functions for Barcode scanner...]
};
我还在我的index.html中包含phonegap.js文件:
I've also included the phonegap.js file in my index.html:
<body>
...
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
</script>
</body>
我真的希望有人能帮我解决我的问题。
I really hope someone can help me solve my problem.
推荐答案
我一直认为你需要先调用
I've always thought that you need to first call
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onError);
为 cordova.file resolveLocalFileSystemURL 。
在您的情况下,尝试修改
So in your case, try this modification
function onFileSystemSuccess() {
store = cordova.file.dataDirectory;
//Check for the file.
window.resolveLocalFileSystemURL(store + fileName, appStart, downloadAsset);
}
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onError);
这篇关于PhoneGap Build:在Android上插件不工作(获取“未定义”错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!