使用预填充的房间数据库时应用程序崩溃

问题描述 投票:0回答:1

我正在尝试使用预填充的 Room 数据库,但在尝试使用该数据库时出现崩溃,并出现以下错误:

 java.lang.NullPointerException: cursor.getString(toColumnIndex) must not be null

一切正常,直到我添加

createFromAsset("databses/test.db")
方法,当它崩溃时。 如果我不执行
getAllCocktails()
方法,就不会发生任何崩溃。 我已经检查过我插入的数据库是否完全相同,并且我找不到任何差异(我还检查了使用 AppInspection 加载的数据)

我尝试使用 Room 的版本 2.5.1 和 2.5.2

活动:

public class PedirTragoActivity extends AppCompatActivity {

    AppDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pedir_trago1_1);

        db = AppDatabase.getInstance(this.getApplication());

        db.cocktailDAO().getAllCocktails();
    }
}

房间数据库:

@Database(entities = {
    /**Entities*/
},
        version = 7)
public abstract class AppDatabase extends RoomDatabase {
    public static AppDatabase INSTANCE;

    
    public abstract CocktailDAO cocktailDAO();

   /**More things*/

    public static AppDatabase getInstance(Context context) {
        if (INSTANCE == null) {
            INSTANCE = Room.databaseBuilder(context, AppDatabase.class, "barbotApp.db")
                    .allowMainThreadQueries()
                    .fallbackToDestructiveMigration()
                    .createFromAsset("databases/test.db")
                    .build();

        }
        return INSTANCE;
    }
}

日志猫:

2023-09-23 17:17:20.600  8250-8250  Choreographer           com.mecatronica.barbot               I  Skipped 67 frames!  The application may be doing too much work on its main thread.
2023-09-23 17:17:20.801  8250-8594  AdrenoGLES-0            com.mecatronica.barbot               I  QUALCOMM build                   : 03e27f8, I326e6aff90
                                                                                                    Build Date                       : 11/02/20
                                                                                                    OpenGL ES Shader Compiler Version: EV031.32.02.04
                                                                                                    Local Branch                     : mybrancheb1d781c-1a78-f1f4-8c78-ac1f6bcc2cee
                                                                                                    Remote Branch                    : quic/gfx-adreno.lnx.1.0.r116-rel
                                                                                                    Remote Branch                    : NONE
                                                                                                    Reconstruct Branch               : NOTHING
2023-09-23 17:17:20.801  8250-8594  AdrenoGLES-0            com.mecatronica.barbot               I  Build Config                     : S P 10.0.7 AArch64
2023-09-23 17:17:20.801  8250-8594  AdrenoGLES-0            com.mecatronica.barbot               I  Driver Path                      : /vendor/lib64/egl/libGLESv2_adreno.so
2023-09-23 17:17:20.813  8250-8594  AdrenoGLES-0            com.mecatronica.barbot               I  PFP: 0x016ee190, ME: 0x00000000
2023-09-23 17:17:21.019  8250-8594  LB                      com.mecatronica.barbot               E  fail to open file: No such file or directory
2023-09-23 17:17:21.021  8250-8594  OpenGLRenderer          com.mecatronica.barbot               I  Davey! duration=1547ms; Flags=1, IntendedVsync=42335033169415, Vsync=42336149836037, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=42336161943432, AnimationStart=42336162004370, PerformTraversalsStart=42336162065047, DrawStart=42336409293120, SyncQueued=42336445054213, SyncStart=42336445465515, IssueDrawCommandsStart=42336445624838, SwapBuffers=42336577074578, FrameCompleted=42336581385203, DequeueBufferDuration=272760, QueueBufferDuration=682187, GpuCompleted=1893829464, 
2023-09-23 17:17:21.027  8250-8250  Looper                  com.mecatronica.barbot               W  PerfMonitor doFrame : time=424ms vsyncFrame=0 latency=1127ms procState=2 historyMsgCount=10 (msgIndex=1 wall=1223ms seq=4 running=1128ms runnable=12ms late=2999ms h=android.app.ActivityThread$H w=159) (msgIndex=5 wall=1007ms seq=8 running=2ms runnable=1ms io=4ms late=3591ms h=android.app.ActivityThread$H w=127)
2023-09-23 17:17:21.154  8250-8250  Looper                  com.mecatronica.barbot               W  PerfMonitor doFrame : time=35ms vsyncFrame=0 latency=459ms procState=2 historyMsgCount=4 (msgIndex=1 wall=424ms seq=14 running=255ms runnable=1ms late=1127ms h=android.view.Choreographer$FrameHandler c=android.view.Choreographer$FrameDisplayEventReceiver) (msgIndex=4 wall=78ms seq=17 running=70ms runnable=2ms late=413ms h=android.view.ViewRootImpl$ViewRootHandler c=androidx.appcompat.app.AppCompatDelegateImpl$2)
2023-09-23 17:17:28.708  8250-8250  MiuiFrameworkFactory    com.mecatronica.barbot               V  get AllImpl object = android.common.MiuiFrameworkFactoryImpl@b038154
2023-09-23 17:17:28.726  8250-8250  MirrorManager           com.mecatronica.barbot               W  this model don't Support
2023-09-23 17:17:28.773  8250-8250  Timeline                com.mecatronica.barbot               I  Timeline: Activity_launch_request time:42344334
2023-09-23 17:17:28.996  8250-8250  DecorView[]             com.mecatronica.barbot               D  getWindowModeFromSystem  windowmode is 1
2023-09-23 17:17:28.997  8250-8250  DecorView               com.mecatronica.barbot               D  createDecorCaptionView windowingMode:1 mWindowMode 1 isFullscreen: true
2023-09-23 17:17:30.631  8250-8250  AndroidRuntime          com.mecatronica.barbot               D  Shutting down VM
2023-09-23 17:17:30.646  8250-8250  AndroidRuntime          com.mecatronica.barbot               E  FATAL EXCEPTION: main
                                                                                                    Process: com.mecatronica.barbot, PID: 8250
                                                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mecatronica.barbot/com.mecatronica.barbot.PedirTragoActivity}: java.lang.NullPointerException: cursor.getString(toColumnIndex) must not be null
                                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3550)
                                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3710)
                                                                                                        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
                                                                                                        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
                                                                                                        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
                                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2146)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                                                        at android.os.Looper.loop(Looper.java:236)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:8057)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
                                                                                                    Caused by: java.lang.NullPointerException: cursor.getString(toColumnIndex) must not be null
                                                                                                        at androidx.room.util.TableInfoKt.readForeignKeyFieldMappings(TableInfo.kt:536)
                                                                                                        at androidx.room.util.TableInfoKt.readForeignKeys(TableInfo.kt:488)
                                                                                                        at androidx.room.util.TableInfoKt.readTableInfo(TableInfo.kt:472)
                                                                                                        at androidx.room.util.TableInfo$Companion.read(TableInfo.kt:130)
                                                                                                        at androidx.room.util.TableInfo.read(Unknown Source:2)
                                                                                                        at com.mecatronica.barbot.database.AppDatabase_Impl$1.onValidateSchema(AppDatabase_Impl.java:136)
                                                                                                        at androidx.room.RoomOpenHelper.onCreate(RoomOpenHelper.kt:72)
                                                                                                        at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onCreate(FrameworkSQLiteOpenHelper.kt:244)
                                                                                                        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:411)
                                                                                                        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:316)
                                                                                                        at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableOrReadableDatabase(FrameworkSQLiteOpenHelper.kt:232)
                                                                                                        at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.innerGetDatabase(FrameworkSQLiteOpenHelper.kt:190)
                                                                                                        at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getSupportDatabase(FrameworkSQLiteOpenHelper.kt:151)
                                                                                                        at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.kt:104)
                                                                                                        at androidx.room.SQLiteCopyOpenHelper.getWritableDatabase(SQLiteCopyOpenHelper.kt:71)
                                                                                                        at androidx.room.RoomDatabase.inTransaction(RoomDatabase.kt:638)
                                                                                                        at androidx.room.RoomDatabase.assertNotSuspendingTransaction(RoomDatabase.kt:457)
                                                                                                        at com.mecatronica.barbot.database.daos.CocktailDAO_Impl.getAllCocktails(CocktailDAO_Impl.java:177)
                                                                                                        at com.mecatronica.barbot.PedirTragoActivity.onCreate(PedirTragoActivity.java:63)
                                                                                                        at android.app.Activity.performCreate(Activity.java:8157)
                                                                                                        at android.app.Activity.performCreate(Activity.java:8129)
                                                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1310)
                                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3523)
                                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3710) 
                                                                                                        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
                                                                                                        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
                                                                                                        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
                                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2146) 
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:106) 
                                                                                                        at android.os.Looper.loop(Looper.java:236) 
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:8057) 
                                                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656) 
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967) 
2023-09-23 17:17:30.716  8250-8250  Process                 com.mecatronica.barbot               I  Sending signal. PID: 8250 SIG: 9
android sqlite android-room
1个回答
0
投票

一切正常,直到我添加 createFromAsset("databses/test.db") 方法

然后从 logcat 中:-

cursor.getString(toColumnIndex) 不能为 null

可能是由于当

@Entity
带注释的类中的列不允许空值时,资产中的数据在列中具有空值(或至少一个空值)。

在您实际尝试检索数据之前,这似乎不会成为问题,因此只有当您从资产引入数据时才会出现问题。

您有两个选择:-

  1. 修改
    @Entity
    带注释的类以允许列为空,或者
  2. 修改源数据以不包含空值。

您可能希望修改您的问题以包括:-

  1. @Entity
    带注释的一个或多个类。
  2. @Dao
    带注释的类。
© www.soinside.com 2019 - 2024. All rights reserved.