我最近经常遇到一些崩溃,但我不知道如何重现或修复它们。这是最常出现的一个
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)
我尝试的每次搜索听起来都像是问题在于尝试从不同线程打开数据库。但是,我认为我有一个设置可以通过在辅助类中使用静态方法来获取当前实例来避免这种情况。
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'.
我尝试更改手机上的区域设置,重新安装应用程序,然后尝试看看是否遇到此问题。我什至尝试在使用其他语言环境安装后切换回英语,但无法重复此崩溃。我有一种感觉,它们可能是相关的,因为它们似乎都是在启动时发生的。