【发布时间】:2013-02-03 11:06:29
【问题描述】:
我刚开始玩PhoneGap的File API,在保存过程中已经遇到了一些问题。这是javascript代码:
function saveCourseToFile() {
console.log("checkpoint 1");
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFSSuccess, onFSError);
}
function onFSSuccess(fileSystem) {
console.log("checkpoint 2");
console.log("Opened file system: " + fileSystem.name);
fileSystem.root.getFile("course.oerk", {create:true, exclusive:false}, gotFileEntry, onFSError);
}
function gotFileEntry(fileEntry) {
console.log("checkpoint 3");
fileEntry.createWriter(gotFileWriter, onFSError);
}
function gotFileWriter(writer) {
writer.onwrite = function(evt) {
console.log("checkpoint 4: write success!");
};
writer.write("test test test");
}
function onFSError(err) {
console.log(err.code);
}
更新:
<script type="text/javascript">
function init() {
document.addEventListener("deviceready", onDeviceReady, false);
}
</script>
<body onload="init()">
<div id="page1">
<a href="#page2">Click me</a>
</div>
<div id="page2">
<script type="text/javascript">
saveCourseToFile();
</script>
<p>Text text text text</p>
</div>
</body>
但是在设备文件系统中没有这样的文件 (course.oerk) 结果。这是一段logcat:
02-18 22:36:55.542: D/Cordova(746): onPageFinished(file:///android_asset/www/index.html#coursesPage)
02-18 22:36:55.542: D/Cordova(746): Trying to fire onNativeReady
02-18 22:36:55.552: D/DroidGap(746): onMessage(onNativeReady,null)
02-18 22:36:55.552: D/DroidGap(746): onMessage(onPageFinished,file:///android_asset/www/index.html#coursesPage)
02-18 22:36:56.223: I/Choreographer(746): Skipped 36 frames! The application may be doing too much work on its main thread.
任何帮助将不胜感激。
【问题讨论】:
-
您是在等到“deviceready”事件发生吗?
-
@SimonMacDonald 我的 onDeviceReady 包含在
<body>标签中,我在移动到特定的<div>标签后尝试调用saveCourseToFile()。详情见代码更新。 -
好的,但是你的错误是什么? logcat 中没有任何内容表明 saveCourseToFile 甚至被调用了。
-
@SimonMacDonald 这里是完整版的 LogCat:dl.dropbox.com/u/72519118/logcat.txt 我也很惊讶 logcat 甚至没有我的检查点。
-
@SimonMacDonald 现在我修好了。即我刚刚将
saveCourseToFile();移动到锚点onclick="saveCourseToFile()"。感谢您的贡献;)