Android WebView 数据库存储返回 null
Android WebView database storage returns null
这是我的问题。我正在尝试将我的 Web 应用程序中的数据本地存储在 HTML5 数据库中。问题是,我尝试用我的javascript打开的数据库返回null。
这是安卓代码:
public class NotepadActivity extends Activity {
WebView main_view = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Get rid of the android title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
main_view = (WebView) findViewById(R.id.main_webview);
WebSettings settings = main_view.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDatabasePath("/data/data/"+this.getPackageName()+"/databases/");
settings.setDomStorageEnabled(true);
main_view.loadUrl("file:///android_asset/index.html");
main_view.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
@Override
public void onReceivedError(WebView view, int errorCod,String description, String failingUrl) {
Toast.makeText(NotepadActivity.this, "Error: " + description , Toast.LENGTH_LONG).show();
}
});
main_view.setWebChromeClient(new WebChromeClient() {
public void onConsoleMessage(String message, int lineNumber, String sourceID) {
Log.d("Notepad", message + " -- From line "
+ lineNumber + " of "
+ sourceID);
}
});
}
public void onExceededDatabaseQuota(String url, String
databaseIdentifier, long currentQuota, long estimatedSize, long
totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
quotaUpdater.updateQuota(estimatedSize * 2);
}
}
以及相关的 JavaScript:
/* DB and result values */
DB_name: "NotepadDB",
DB_version: "1",
DB_displayName: "Notepad Database",
DB_sizeEstimate: 5 * 1024 * 1024,
DB: null,
....
/* Open a connection to the DB (or create if it doesn't exist) */
openConnection: function() {
App.log("App > DB > openConnection > Attempting to access database");
App.val.DB = window.openDatabase(App.val.DB_name, App.val.DB_version,
App.val.DB_displayName, App.val.DB_sizeEstimate, App.DB.initializeDatabase);
},
/* Only called when the database is initially created */
initializeDatabase: function(database) {
App.val.DB = database;
//Create the required table
App.val.DB.transaction(
function (tx) {tx.executeSql('CREATE TABLE ' + App.schema.tableName + ' (' + App.schema.ID+ ' int unique, ' + App.schema.title + ' text, ' + App.schema.content + ' text, ' + App.schema.date + ' datetime)',
[],
function (tx, res) {
App.log("App > DB > intializeDatabase > Table Created Successfully");
App.DB.loadAllNotes();
},
function (tx, err) {
App.log("App > DB > intializeDatabase > ERROR - Table creation failed - code: " + err.code + ", message: " + err.message);
App.error("Unable to create database. Please try again later.");
});
}
);
},
loadAllNotes: function() {
if(App.val.DB != null) {
//Do some stuff
} else {
App.log("App > DB > loadAllNotes > Database was NULL");
}
}
在我的日志猫日志中,我看到 App.log("App> DB> loadAllNotes> Database is NULL");正在执行。
我在这里缺少什么吗?为什么数据库为空?
解决了。问题是我忽略了以下代码:
public void onExceededDatabaseQuota(String url, String
databaseIdentifier, long currentQuota, long estimatedSize, long
totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
quotaUpdater.updateQuota(estimatedSize * 2);
}
此函数应位于main_view.setWebChromeClient(new WebChromeClient() {
函数内,因为它需要重写。哎呀!
创建 SQlite 数据库文件并将其放入资产目录并在首次运行应用程序时将文件加载到 SDcard 上会更容易。 你可以使用一个火狐插件作为数据库编辑器。这将需要更少的应用程序中的代码。 您可以使用我在SO上发布的方法。只需使用 .sql 文件而不是 txt 文件 - 这就是我对我的应用程序所做的。
相关文章:
- Meteor方法在客户端返回null,在客户端运行的相同方法返回正确的值
- php函数的ajax html$_POST值返回null
- 是否可以 document.getElementsByTagName('head')[0] ever 返回 null
- Firebase:查询返回null
- 在浅层渲染的反应组件上,Enzyme为道具返回null
- jQuery Multiselect插件返回null长度(应为0?)
- 如何处理getElementById返回Null
- JavaScript中的RegExp返回null
- Javascript 中的 RegEx 返回 Null
- 加载时的getelementbyID返回null
- 谷歌脚本在第二次调用Gmail后返回null
- Indexeddb OpenCursor在特定键值上返回null
- Ajax响应返回null
- 有没有一种方法可以判断图像src是否在javascript内部返回null
- 当JS在Chrome控制台上正常工作时,Selenium JavaScript执行器返回null
- Auth$getUser返回null,即使用户已登录(AngularFire,Firebase)
- Mongoose JS findOne总是返回null
- 为什么我的ajax调用返回null
- 当与全局标志一起使用时,Javascript Regex在后续调用中返回null
- Regex以确定该值在JS中是否有效,返回null