【问题标题】:PhoneGap Build: Plugins not working (getting "undefined" errors) on AndroidPhoneGap Build:插件在 Android 上不起作用(出现“未定义”错误)
【发布时间】:2014-12-20 05:36:15
【问题描述】:

我在这里遇到了一个非常讨厌的问题。每次,我尝试在我的项目中使用文件和文件传输插件时,在尝试使用 @987654321 获取数据目录时,都会收到 Uncaught TypeError: Cannot read property 'dataDirectory' of undefined 错误@ .现在,我已经尝试解决我的问题几个小时,但我找不到任何帮助。我还在这个项目中使用 Barcode Scanner 插件,它的工作原理非常棒。我正在使用 PhoneGap 3.6.3 和 jQuery Mobile 1.4.4。由于我是 PhoneGap 的新手,我很可能遗漏了一些重要的东西......

我已将插件包含在我的 config.xml 中,如下所示:

<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 构建日志正在确认正在添加插件。但似乎它们没有被添加到 Android 构建中,因为我在构建日志中找不到它们的任何引用。

Windows Phone 日志:

添加 www\plugins\org.apache.cordova.file-transfer\www\FileTransfer.js

添加 www\plugins\org.apache.cordova.file-transfer\www\FileTransferError.js

添加 www\plugins\org.apache.cordova.file\www\File.js

这是我的 index.js 的一部分,不包括条形码扫描仪功能。

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 文件:

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

我真的希望有人能帮我解决我的问题。

【问题讨论】:

    标签: cordova phonegap-plugins phonegap-build


    【解决方案1】:

    我一直认为你需要先打电话

    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onError);
    

    用于cordova.file 的东西被定义并能够调用resolveLocalFileSystemURL

    所以在你的情况下,试试这个修改

    function onFileSystemSuccess() {
        store = cordova.file.dataDirectory;
    
        //Check for the file. 
        window.resolveLocalFileSystemURL(store + fileName, appStart, downloadAsset);
    }
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onError);
    

    【讨论】:

    • 感谢您的回复。我应用了您的修改,现在显示为Uncaught ReferenceError: LocalFileSystem is not defined。 :D
    • 哦,抱歉,从我的生产代码中复制了它,其中 LocalFileSystem.PERSISTENT 等于 1,所以请将其更改为 1 来解决这个问题。
    • 很遗憾,这并没有解决问题。你还有别的想法吗?我不能再直接思考了。但我明天再检查。也许,我必须将代码减少到 cordova.filecordova.file-transfer 的东西才能找到问题。
    • 好吧,在这种情况下,你确定你的 deviceready 事件被触发了吗?由于奇怪的原因,我很难让它不触发。当我开始使用GapDebug 远程调试我的应用程序时,我终于能够对我的问题有所了解,如果还没有使用构建应用程序的远程调试,你应该尝试一下。
    • @tyso89:你能解决这个问题吗?很高兴在这里获得最终答案以供将来参考! :)
    猜你喜欢
    • 2017-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多