最近,我遇到了一些经常发生的撞车事故,我想不出如何复制它们或修复它们。这是最常出现的一个
Fatal Exception: java.lang.RuntimeException: Unable to create application com.myapp.MyApplication: android.database.sqlite.SQLiteDiskIOException: disk I/O error (code 1802): , while compiling: PRAGMA journal_mode
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5436)
at android.app.ActivityThread.-wrap2(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1546)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6154)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Caused by android.database.sqlite.SQLiteDiskIOException: disk I/O error (code 1802): , while compiling: PRAGMA journal_mode
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(SQLiteConnection.java)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:634)
at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:320)
at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:294)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:215)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:808)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:793)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:696)
at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:680)
at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:289)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.myapp.DBConnect.getInstance(DBConnect.java:202)
at com.myapp.MyApplication.onCreate(MyApplication.java:66)我尝试的每一次搜索都让人觉得问题就在于尝试从不同的线程打开数据库。但是,我认为通过在helper类中有一个静态方法来获取当前实例来避免这种情况。
private static DBConnect mInstance = null;
private static SQLiteDatabase db;
public static DBConnect getInstance(Context ctx) {
if (mInstance == null) {
mInstance = new DBConnect(ctx.getApplicationContext());
db = mInstance.getWritableDatabase(); // this is line 202 in DBConnect.java
// all methods in this class reference this db var to run database queries
}
return mInstance;
}然后,在需要访问数据库的任何地方,都可以使用以下行设置局部变量db:
// this is line 66 in MyApplication.java
DBConnect db = DBConnect.getInstance(this);然后使用像db.addItem()这样的函数运行所有的数据库查询
它一直工作得很好,我90%肯定多个线程正在访问相同的表,而我从未见过崩溃。该应用程序有几个地方的背景同步列表,最容易复制的是当应用程序第一次启动时,它执行这些列表的同步。在同步时,用户可以查看不同的列表,加载每个列表将从数据库中选择其内容,可能是在后台同步处理中从相同的表中添加或删除项目时。我每天也有几百名活跃用户,在过去的几个月里,我只见过15次这样的崩溃。
而且,看到错误的第一行是java.lang.RuntimeException: Unable to create application ...,这似乎发生在用户第一次启动应用程序时?不确定此时如何已经存在多个线程,除非用户已登录,否则所有后台进程都不会运行。
我还看到由同一行引起的下列错误:
Failed to change locale for db '/data/user/0/com.myapp/databases/mydb' to 'en_US'.我已经尝试改变我的手机上的地区设置,重新安装应用程序,然后混乱,看看我是否有这个问题。我甚至尝试在使用另一个地区安装后切换回英语,但是没有能够重复这次崩溃。我有一种感觉,他们可能是相关的,因为他们似乎都发生在创业。
发布于 2017-07-26 14:12:29
在我的例子中,我没有给予运行时许可。我将数据库文件保存在外部存储中,当请求许可时,我拒绝了它,但是当允许许可时,它就正常工作了。在SDK版本>= 23上,您必须给予运行时权限。
https://stackoverflow.com/questions/44312563
复制相似问题