Android WebView抛出SQLiteException

时间:2011-12-29 01:52:34

标签: android sqlite android-webview

我在应用程序中从零星的SQLiteException获取崩溃报告。我根本不直接与SQLite数据库交互。我已将代码跟踪到WebView小部件的使用情况。例外情况不同,但有几个例子如下......

android.database.sqlite.SQLiteException: database is locked: BEGIN EXCLUSIVE;
at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1768)
at android.database.sqlite.SQLiteDatabase.beginTransactionWithListener(SQLiteDatabase.java:558)
at android.database.sqlite.SQLiteDatabase.beginTransaction(SQLiteDatabase.java:512)
at android.webkit.WebViewDatabase.startCacheTransaction(WebViewDatabase.java:603)
at android.webkit.CacheManager.enableTransaction(CacheManager.java:251)
at android.webkit.WebViewWorker.handleMessage(WebViewWorker.java:214)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.os.HandlerThread.run(HandlerThread.java:60)

另一个例子......

java.lang.RuntimeException: Unable to create application com.example.MyApplication: android.database.sqlite.SQLiteException: near "VALUES": syntax error: , while compiling: INSERT INTO cache (VALUES (
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3828)
at android.app.ActivityThread.access$2200(ActivityThread.java:132)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1082)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:4293)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: near "VALUES": syntax error: , while compiling: INSERT INTO cache (VALUES (
at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41)
at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1231)
at android.database.DatabaseUtils$InsertHelper.getStatement(DatabaseUtils.java:858)
at android.database.DatabaseUtils$InsertHelper.getColumnIndex(DatabaseUtils.java:904)
at android.webkit.WebViewDatabase.getInstance(WebViewDatabase.java:397)
at android.webkit.WebView.<init>(WebView.java:1077)
at android.webkit.WebView.<init>(WebView.java:1054)
at android.webkit.WebView.<init>(WebView.java:1044)
at android.webkit.WebView.<init>(WebView.java:1035)
at com.example.MyApplication.getWebView(MyApplication.java:223)
at com.example.MyApplication.loadUrlInWebView(MyApplication.java:249)
at com.example.MyApplication.onCreate(MyApplication.java:169)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:984)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3825)
... 10 more

为什么会发生这些崩溃?还有其他人有这个问题吗?

注意:错误报告中的平台每个都列为 OTHER ,因此这可能只发生在操作系统的模拟器或非正式版本中。

1 个答案:

答案 0 :(得分:2)

这可能与webview DOM存储有关。请参阅WebSettings.setDatabaseEnabled和WebSettings.setDatabasePath。

http://developer.android.com/reference/android/webkit/WebSettings.html#setDatabaseEnabled(boolean) http://developer.android.com/reference/android/webkit/WebSettings.html#setDatabasePath(java.lang.String)

一个常见的抱怨是DOM存储不起作用,通常用以下代码修复:

mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setDatabaseEnabled(true);
mWebView.getSettings().setDatabasePath("/data/data/packagename/databases/");

我从来没有听说过这方面的崩溃问题,但是当您禁用DOM存储时以及指定正确的数据库路径(如上所示使用应用程序的包名称)时,可能值得研究一下。

此主题也可能有所帮助:
Android - Making Webview DomStorage persistant after app closed