迁移到房间,应用程序在启动时崩溃,异常

时间:2018-01-30 04:03:38

标签: android android-room

以下是该应用的代码: https://github.com/sandy-8925/Checklist/tree/room_db

当我构建并启动它时,应用程序始终崩溃并出现以下异常。请注意,我已经使用普通的Sqlite DB

获得了此应用程序的先前版本
  

引起:java.lang.IllegalStateException:迁移不正确   处理项目(org.sanpra.checklist.activity.ChecklistItem)。                                                                          预期:                                                                         TableInfo {name ='items',columns = {checked = Column {name ='checked',   type ='INTEGER',notNull = true,primaryKeyPosition = 0},   _id =列{name ='_ id',type ='INTEGER',notNull = true,primaryKeyPosition = 1},desc = Column {name ='desc',type ='TEXT',   notNull = false,primaryKeyPosition = 0}},foreignKeys = [],indices = []}                                                                          实测:                                                                         TableInfo {name ='items',columns = {checked = Column {name ='checked',   type ='integer',notNull = true,primaryKeyPosition = 0},   _id =列{name ='_ id',type ='integer',notNull = false,primaryKeyPosition = 1},desc = Column {name ='desc',type ='text',   notNull = true,primaryKeyPosition = 0}},foreignKeys = [],indices = []}

     

在   org.sanpra.checklist.dbhelper.ItemsDatabase_Impl $ 1.validateMigration(ItemsDatabase_Impl.java:66)                                                                             在   android.arch.persistence.room.RoomOpenHelper.onUpgrade(RoomOpenHelper.java:75)                                                                             在   android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper $ OpenHelper.onUpgrade(FrameworkSQLiteOpenHelper.java:118)                                                                             在   android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:299)                                                                             在   android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:194)                                                                             在   android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper $ OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:93)                                                                             在   android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:54)                                                                             在   android.arch.persistence.room.RoomDatabase.query(RoomDatabase.java:193)                                                                             在   org.sanpra.checklist.activity.ItemsDao_Impl.fetchAllItems(ItemsDao_Impl.java:229)                                                                             在   org.sanpra.checklist.activity.ChecklistItemsCursorLoader.loadInBackground(ChecklistItemsCursorLoader.java:46)                                                                             在   org.sanpra.checklist.activity.ChecklistItemsCursorLoader.loadInBackground(ChecklistItemsCursorLoader.java:32)                                                                             在   android.support.v4.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:306)                                                                             在   android.support.v4.content.AsyncTaskLoader $ LoadTask.doInBackground(AsyncTaskLoader.java:59)                                                                             在   android.support.v4.content.AsyncTaskLoader $ LoadTask.doInBackground(AsyncTaskLoader.java:47)                                                                             在   android.support.v4.content.ModernAsyncTask $ 2.call(ModernAsyncTask.java:138)                                                                             在java.util.concurrent.FutureTask.run(FutureTask.java:266)

1 个答案:

答案 0 :(得分:1)

预期

  

TableInfo {name ='items',columns = {checked = Column {name ='checked',   type ='INTEGER',notNull = true,primaryKeyPosition = 0},   _id =列{name ='_ id',type ='INTEGER', notNull = true ,primaryKeyPosition = 1},desc = Column {name ='desc',type ='TEXT',    notNull = false ,primaryKeyPosition = 0}},foreignKeys = [],indices = []}

实测值

  

TableInfo {name ='items',columns = {checked = Column {name ='checked',   type ='integer',notNull = true,primaryKeyPosition = 0},   _id =列{name ='_ id',type ='integer', notNull = false ,primaryKeyPosition = 1},desc =列{name ='desc',type ='text',    notNull = true ,primaryKeyPosition = 0}},foreignKeys = [],indices = []}

_id可以为空,但是Room期望它不为空。

desc不为null但是Room期望它为null