English 中文(简体)
使用预设室数据库时的坠毁情况
原标题:App crashes when using prepopulated room database

我正试图使用一个预先安装的会议室数据库,但在试图使用数据库时,我遇到过错: java.lang.NullPointerException: cursor.getString(toColumnIndex)绝不能成为

It all works fine until I add the createFromAsset("databses/test.db") method, when it crashes. Nothing crashes if I just don t execute the getAllCocktails() method. I already checked if the DB I inserted has all the same and I couldn t find any diferences(I also checked data gets loaded with AppInspection)

I don t only test with getAllCocktails ( 我还尝试用完全瘫痪的构造加插

I will post (some of)my code and logs so it is easier to diagnose I tried with Room s version 2.5.1 and 2.5.2

test.db 档案

活动:

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 = {
        BottleEntity.class,
        BottleIngredientEntity.class,
        CocktailEntity.class,
        CocktailIngredientEntity.class,
        IngredientTypeEntity.class
},
        version = 7)
public abstract class AppDatabase extends RoomDatabase {
    public static AppDatabase INSTANCE;

    public abstract BottleDAO bottleDAO();

    public abstract BottleIngredientDAO bottleIngredientDAO();

    public abstract CocktailDAO cocktailDAO();

    public abstract CocktailIngredientDAO cocktailIngredientDAO();

    public abstract IngredientTypeDAO ingredientTypeDAO();
    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;
    }
}

BottleEntity:

@Entity(tableName = "bottles")
public class BottleEntity {
    @PrimaryKey
    @NonNull
    private int position;//Del 0 al 7 son alcoholes, el 8 es shaker y del 9 al 16 son mezclas
    private String name;
    @NonNull
    private int capacity;
    @NonNull
    private int currentAmount;

    public BottleEntity() {
    }

    public BottleEntity(int position, String name, int capacity, int currentAmount) {
        this.position = position;
        this.name = name;
        this.capacity = capacity;
        this.currentAmount = currentAmount;
    }
    @Ignore
    public BottleEntity(int position, String name, int capacity) {
        this.position = position;
        this.name = name;
        this.capacity = capacity;
        this.currentAmount = capacity;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCapacity() {
        return capacity;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

    public int getCurrentAmount() {
        return currentAmount;
    }

    public void setCurrentAmount(int currentAmount) {
        this.currentAmount = currentAmount;
    }
}

BottleIngredientEntity:

@Entity(tableName = "bottle_ingredient",
        foreignKeys = {
                @ForeignKey(entity = BottleEntity.class, parentColumns = "position", childColumns = "bottlePos"),
                @ForeignKey(entity = IngredientTypeEntity.class, parentColumns = "id", childColumns = "ingredientId")
        })
public class BottleIngredientEntity {

    @PrimaryKey(autoGenerate = true)
    @NonNull
    private int id;

    @NonNull
    private int bottlePos;


    @NonNull
    private int ingredientId;


    public BottleIngredientEntity() {
    }

    public BottleIngredientEntity(int bottlePos, int ingredientId) {
        this.bottlePos = bottlePos;
        this.ingredientId = ingredientId;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getBottlePos() {
        return bottlePos;
    }

    public void setBottlePos(int bottlePos) {
        this.bottlePos = bottlePos;
    }

    public int getIngredientId() {
        return ingredientId;
    }

    public void setIngredientId(int ingredientId) {
        this.ingredientId = ingredientId;
    }
}

CocktailIngredientEntity:

@Entity(tableName = "cocktail_ingredients",
        foreignKeys = {
                @ForeignKey(entity = IngredientTypeEntity.class, parentColumns = "id", childColumns = "typeId"),
                @ForeignKey(entity = CocktailEntity.class, parentColumns = "id", childColumns = "cocktailId")
        })
public class CocktailIngredientEntity {
    @PrimaryKey(autoGenerate = true)
    @NonNull
    private int id;
    @NonNull
    private int typeId;
    @NonNull
    private int quantity;
    @NonNull
    private int cocktailId;

    public CocktailIngredientEntity(int typeId, int quantity, int cocktailId) {
        this.typeId = typeId;
        this.quantity = quantity;
        this.cocktailId = cocktailId;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getTypeId() {
        return typeId;
    }

    public void setTypeId(int typeId) {
        this.typeId = typeId;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public int getCocktailId() {
        return cocktailId;
    }

    public void setCocktailId(int cocktailId) {
        this.cocktailId = cocktailId;
    }
}

兴奋剂

@Entity(tableName = "ingredient_types")
public class IngredientTypeEntity {
    @PrimaryKey(autoGenerate = true)
    @NonNull
    private int id;
    private String name;

    public IngredientTypeEntity() {
    }

    public IngredientTypeEntity(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

CocktailEntity:

@Entity(tableName = "cocktails")
public class CocktailEntity {
    @PrimaryKey(autoGenerate = true)
    @NonNull
    private int id;

    private String name;

    @NonNull
    private boolean isOnStock;
    @NonNull
    private boolean hasIce;

    private String imgName;

    public CocktailEntity(String name, boolean isOnStock, boolean hasIce, String imgName) {
        this.name = name;
        this.isOnStock = isOnStock;
        this.hasIce = hasIce;
        this.imgName = imgName;
    }

    @Ignore
    public CocktailEntity(String name, boolean hasIce, String imaName) {
        this.name = name;
        this.isOnStock = false;
        this.hasIce = hasIce;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isOnStock() {
        return isOnStock;
    }

    public void setOnStock(boolean onStock) {
        isOnStock = onStock;
    }

    public boolean isHasIce() {
        return hasIce;
    }

    public void setHasIce(boolean hasIce) {
        this.hasIce = hasIce;
    }

    public String getImgName() {
        return imgName;
    }

    public void setImgName(String imgName) {
        this.imgName = imgName;
    }
}

Cocktail Dao:

@Dao
public interface CocktailDAO {

    @Insert
    long insertCocktail(CocktailEntity cocktail);

    @Query("SELECT id FROM cocktails WHERE name = :name")
    int getCocktailId(String name);

    @Query("SELECT c.* FROM cocktails AS c WHERE c.id = :id")
    CocktailEntity getCocktail(int id); //Need to use a repository, separate this 2 parts

    @Query("SELECT * FROM cocktails")
    List<CocktailEntity> getAllCocktails();

    @Query("SELECT cocktails.id FROM cocktails")
    List<Integer> getAllCocktailIds();

    @Query("SELECT * FROM cocktails" +
            " WHERE cocktails.isOnStock = 1")
    List<CocktailEntity> getAllCocktailsInStock();

    @Query("SELECT isOnStock FROM cocktails WHERE id = :id")
    boolean isCocktailInStock(int id);

    @Query("UPDATE cocktails SET isOnStock = :isOnStock WHERE id = :id")
    void updateCocktailStock(int id, boolean isOnStock);
}

清单:

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

qlump form test.db:

PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS "ingredient_types"
(
    id   INTEGER not null
        constraint ingredient_types_pk
            primary key,
    name TEXT
);
INSERT INTO ingredient_types VALUES(1, SHAKE );
INSERT INTO ingredient_types VALUES(2, limón );
INSERT INTO ingredient_types VALUES(3, ron );
INSERT INTO ingredient_types VALUES(4, tonica );
INSERT INTO ingredient_types VALUES(5, ginebra );
INSERT INTO ingredient_types VALUES(6, campari );
INSERT INTO ingredient_types VALUES(7, naranja );
INSERT INTO ingredient_types VALUES(8, fernet );
INSERT INTO ingredient_types VALUES(9, coca );
INSERT INTO ingredient_types VALUES(10, durazno );
INSERT INTO ingredient_types VALUES(11, granadina );
INSERT INTO ingredient_types VALUES(12, vodka );
CREATE TABLE IF NOT EXISTS "bottle_ingredient"
(
    id           INTEGER not null
        constraint bottle_ingredient_pk
            primary key,
    bottlePos    INTEGER not null
        constraint bottle_ingredient_bottles_position_fk
            references bottles,
    ingredientId INTEGER not null
        constraint bottle_ingredient_ingredient_types_id_fk
            references ingredient_types
);
INSERT INTO bottle_ingredient VALUES(1,8,5);
INSERT INTO bottle_ingredient VALUES(2,9,6);
INSERT INTO bottle_ingredient VALUES(3,10,8);
INSERT INTO bottle_ingredient VALUES(4,11,3);
INSERT INTO bottle_ingredient VALUES(5,12,12);
INSERT INTO bottle_ingredient VALUES(7,0,2);
INSERT INTO bottle_ingredient VALUES(8,1,4);
INSERT INTO bottle_ingredient VALUES(9,2,7);
INSERT INTO bottle_ingredient VALUES(10,3,9);
INSERT INTO bottle_ingredient VALUES(11,4,10);
INSERT INTO bottle_ingredient VALUES(12,5,11);
INSERT INTO bottle_ingredient VALUES(13,16,1);
CREATE TABLE IF NOT EXISTS "bottles"
(
    position      INTEGER not null
        constraint bottles_pk
            primary key,
    name          TEXT,
    capacity      INTEGER not null,
    currentAmount INTEGER not null
);
INSERT INTO bottles VALUES(0, limon ,2000,2000);
INSERT INTO bottles VALUES(1, tonica ,2000,2000);
INSERT INTO bottles VALUES(2, naranja ,2000,2000);
INSERT INTO bottles VALUES(3, coca ,2000,2000);
INSERT INTO bottles VALUES(4, durazno ,2000,2000);
INSERT INTO bottles VALUES(5, granadina ,2000,2000);
INSERT INTO bottles VALUES(6, Mezcla 7 ,1,1);
INSERT INTO bottles VALUES(7, Mezcla 8 ,1,1);
INSERT INTO bottles VALUES(8, ginebra ,700,700);
INSERT INTO bottles VALUES(9, campari ,750,750);
INSERT INTO bottles VALUES(10, fernet ,750,750);
INSERT INTO bottles VALUES(11, ron ,750,750);
INSERT INTO bottles VALUES(12, vodka ,700,700);
INSERT INTO bottles VALUES(13, Bottle 6 ,1,1);
INSERT INTO bottles VALUES(14, Bottle 7 ,1,1);
INSERT INTO bottles VALUES(15, Bottle 8 ,1,1);
INSERT INTO bottles VALUES(16, Shakeo ,1000,1000);
CREATE TABLE IF NOT EXISTS "cocktail_ingredients"
(
    id         INTEGER not null
        constraint cocktail_ingredients_pk
            primary key,
    typeId     INTEGER not null
        constraint cocktail_ingredients_ingredient_types_id_fk
            references ingredient_types,
    quantity   INTEGER not null,
    cocktailId INTEGER not null
        constraint cocktail_ingredients_cocktails_id_fk
            references cocktails
);
INSERT INTO cocktail_ingredients VALUES(1,6,60,1);
INSERT INTO cocktail_ingredients VALUES(2,7,136,1);
INSERT INTO cocktail_ingredients VALUES(3,9,146,2);
INSERT INTO cocktail_ingredients VALUES(4,8,50,2);
INSERT INTO cocktail_ingredients VALUES(5,5,60,3);
INSERT INTO cocktail_ingredients VALUES(6,4,136,3);
INSERT INTO cocktail_ingredients VALUES(7,3,50,4);
INSERT INTO cocktail_ingredients VALUES(8,9,120,4);
INSERT INTO cocktail_ingredients VALUES(9,2,10,4);
INSERT INTO cocktail_ingredients VALUES(10,12,40,5);
INSERT INTO cocktail_ingredients VALUES(11,10,20,5);
INSERT INTO cocktail_ingredients VALUES(12,7,40,5);
INSERT INTO cocktail_ingredients VALUES(13,1,30,5);
INSERT INTO cocktail_ingredients VALUES(14,11,40,5);
CREATE TABLE IF NOT EXISTS "cocktails"
(
    id        INTEGER not null
        constraint cocktails_pk
            primary key,
    name      TEXT,
    isOnStock INTEGER not null,
    hasIce    INTEGER not null,
    imgName   TEXT
);
INSERT INTO cocktails VALUES(1, campari ,1,1, campari );
INSERT INTO cocktails VALUES(2, fernet ,1,1, fernet );
INSERT INTO cocktails VALUES(3, gin tonic ,1,1, gintonic );
INSERT INTO cocktails VALUES(4, ron cola ,1,1, roncola );
INSERT INTO cocktails VALUES(5, sex On  the beach ,1,1, sexOnTheBeach );
COMMIT;
问题回答

在我添加“FromAsset(数据bses/test.db)”方法之前,所有工作都得到了罚款。

之后改为:

cursor.getString(toColumnIndex) must not be null

Is probably due to the data from the asset having nulls (or at least one null) in a column when the column in the @Entity annotated class does not allow nulls.

还应指出的是,如果你排除了<代码>db.cocktailDAO().getAllCocktails(>);,那么数据库甚至不会进入,因此甚至不会建立。 这只是一个数据库的例子,没有开放数据库,开放时间被推迟,直到试图进入数据库。

在你实际努力检索数据之前,这似乎是一个问题,因此,只有在你介绍资产数据时,才会出现这一问题。

你有两种选择:

  1. modify the @Entity annotated class to allow nulls for the column(s), or
  2. modify the source data to not have nulls.

请修改您的问题,包括:

  1. The @Entity annotated class or classes.
  2. The @Dao annotated class.

www.un.org/Depts/DGACM/index_spanish.htm 显示这一问题的工作实例很可能与资产有关。

利用你的代码(除了使用主要积极性而不是PedirTragoActative)创建的AS项目,并修改了主要积极性如下:

public class MainActivity extends AppCompatActivity {

    AppDatabase db;
    String TAG = "DBINFO";

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

        db = AppDatabase.getInstance(this.getApplication());
        logAllCocktails(db.cocktailDAO().getAllCocktails(),"STG01");
        db.cocktailDAO().insertCocktail(new CocktailEntity("C1",false,"Blah1"));
        db.cocktailDAO().insertCocktail(new CocktailEntity("C2",true,"Blah2"));
        db.cocktailDAO().insertCocktail(new CocktailEntity("C3",false,"Blah3"));
        logAllCocktails(db.cocktailDAO().getAllCocktails(),"STG02");
        db.close();
    }

    void logAllCocktails(List<CocktailEntity> allCocktails,String tag_suffix) {
        Log.d(TAG +tag_suffix,"Starting Log of All Cocktails where supplied list has " + allCocktails.size() + " items.");
        for (CocktailEntity ce: allCocktails) {
            Log.d(
                    TAG+tag_suffix,
                    "Cocktail Name is " + ce.getName() + " HasIce is " + ce.isHasIce() + " ID is " + ce.getId() + " ImageName is " + ce.getImgName()
            );
        }
    }
}

2. 以上两度为创作 评论。 记录显示第2部分C类尾料,后3项添加3项Ccktails。 第二次调查显示,3和6。

注 添加了近距离(这迫使WAL仅承付一个档案)。

旧装置探测器将数据库档案复制到资产夹中,然后作为测试.db,例如:-

“entergram

申请表的未结之处包括: 从业守则和再理论:-

Resultantlog:-

2023-09-24 07:51:34.929 21370-21370/a.a.so77164866javaroomnpefromasset D/DBINFOSTG01: Starting Log of All Cocktails where supplied list has 6 items.
2023-09-24 07:51:34.929 21370-21370/a.a.so77164866javaroomnpefromasset D/DBINFOSTG01: Cocktail Name is C1 HasIce is false ID is 1 ImageName is null
2023-09-24 07:51:34.929 21370-21370/a.a.so77164866javaroomnpefromasset D/DBINFOSTG01: Cocktail Name is C2 HasIce is true ID is 2 ImageName is null
2023-09-24 07:51:34.929 21370-21370/a.a.so77164866javaroomnpefromasset D/DBINFOSTG01: Cocktail Name is C3 HasIce is false ID is 3 ImageName is null
2023-09-24 07:51:34.929 21370-21370/a.a.so77164866javaroomnpefromasset D/DBINFOSTG01: Cocktail Name is C1 HasIce is false ID is 4 ImageName is null
2023-09-24 07:51:34.929 21370-21370/a.a.so77164866javaroomnpefromasset D/DBINFOSTG01: Cocktail Name is C2 HasIce is true ID is 5 ImageName is null
2023-09-24 07:51:34.929 21370-21370/a.a.so77164866javaroomnpefromasset D/DBINFOSTG01: Cocktail Name is C3 HasIce is false ID is 6 ImageName is null
2023-09-24 07:51:34.953 21370-21370/a.a.so77164866javaroomnpefromasset D/DBINFOSTG02: Starting Log of All Cocktails where supplied list has 9 items.
2023-09-24 07:51:34.953 21370-21370/a.a.so77164866javaroomnpefromasset D/DBINFOSTG02: Cocktail Name is C1 HasIce is false ID is 1 ImageName is null
2023-09-24 07:51:34.953 21370-21370/a.a.so77164866javaroomnpefromasset D/DBINFOSTG02: Cocktail Name is C2 HasIce is true ID is 2 ImageName is null
2023-09-24 07:51:34.953 21370-21370/a.a.so77164866javaroomnpefromasset D/DBINFOSTG02: Cocktail Name is C3 HasIce is false ID is 3 ImageName is null
2023-09-24 07:51:34.953 21370-21370/a.a.so77164866javaroomnpefromasset D/DBINFOSTG02: Cocktail Name is C1 HasIce is false ID is 4 ImageName is null
2023-09-24 07:51:34.953 21370-21370/a.a.so77164866javaroomnpefromasset D/DBINFOSTG02: Cocktail Name is C2 HasIce is true ID is 5 ImageName is null
2023-09-24 07:51:34.953 21370-21370/a.a.so77164866javaroomnpefromasset D/DBINFOSTG02: Cocktail Name is C3 HasIce is false ID is 6 ImageName is null
2023-09-24 07:51:34.953 21370-21370/a.a.so77164866javaroomnpefromasset D/DBINFOSTG02: Cocktail Name is C1 HasIce is false ID is 7 ImageName is null
2023-09-24 07:51:34.953 21370-21370/a.a.so77164866javaroomnpefromasset D/DBINFOSTG02: Cocktail Name is C2 HasIce is true ID is 8 ImageName is null
2023-09-24 07:51:34.953 21370-21370/a.a.so77164866javaroomnpefromasset D/DBINFOSTG02: Cocktail Name is C3 HasIce is false ID is 9 ImageName is null
  • i.e. NO NPE Crash
  • Originally the 6 rows from the previous runs, then
  • 9 rows after 3 new rows inserted.
  • Note the constrcutor public CocktailEntity(String name, boolean hasIce, String imaName) was used, hence the nulls for imageName (and that these being null are not an issue)

www.un.org/Depts/DGACM/index_spanish.htm Edit利用下载的媒体档案测试。

简言之,这是罚款和工程(第1版和第7版)。

因此,问题似乎并不在于所提供的编码(数据库是明智的)或档案本身。

i. 即,在第七版,新安装后,(根据上述代码)的标识结果如下:

2023-09-24 12:01:19.596 D/DBINFOSTG01: Starting Log of All Cocktails where supplied list has 5 items.
2023-09-24 12:01:19.596 D/DBINFOSTG01: Cocktail Name is campari HasIce is true ID is 1 ImageName is campari
2023-09-24 12:01:19.596 D/DBINFOSTG01: Cocktail Name is fernet HasIce is true ID is 2 ImageName is fernet
2023-09-24 12:01:19.596 D/DBINFOSTG01: Cocktail Name is gin tonic HasIce is true ID is 3 ImageName is gintonic
2023-09-24 12:01:19.596 D/DBINFOSTG01: Cocktail Name is ron cola HasIce is true ID is 4 ImageName is roncola
2023-09-24 12:01:19.596 D/DBINFOSTG01: Cocktail Name is sex On  the beach HasIce is true ID is 5 ImageName is sexOnTheBeach
2023-09-24 12:01:19.608 D/DBINFOSTG02: Starting Log of All Cocktails where supplied list has 8 items.
2023-09-24 12:01:19.608 D/DBINFOSTG02: Cocktail Name is campari HasIce is true ID is 1 ImageName is campari
2023-09-24 12:01:19.608 D/DBINFOSTG02: Cocktail Name is fernet HasIce is true ID is 2 ImageName is fernet
2023-09-24 12:01:19.608 D/DBINFOSTG02: Cocktail Name is gin tonic HasIce is true ID is 3 ImageName is gintonic
2023-09-24 12:01:19.608 D/DBINFOSTG02: Cocktail Name is ron cola HasIce is true ID is 4 ImageName is roncola
2023-09-24 12:01:19.609 D/DBINFOSTG02: Cocktail Name is sex On  the beach HasIce is true ID is 5 ImageName is sexOnTheBeach
2023-09-24 12:01:19.609 D/DBINFOSTG02: Cocktail Name is C1 HasIce is false ID is 6 ImageName is null
2023-09-24 12:01:19.609 D/DBINFOSTG02: Cocktail Name is C2 HasIce is true ID is 7 ImageName is null
2023-09-24 12:01:19.609 D/DBINFOSTG02: Cocktail Name is C3 HasIce is false ID is 8 ImageName is null

因此,这个问题可能产生两种申请之间的差别,后者可能很多。

  • The device (works on AS emulator for 10.1 device API 28)
  • The Activity (very simple initial Activity without anything special layout wise) i.e. the layouts for the Activity are different.
  • You have Entities commented out in the posted code BUT do you have the other entities excluded when testing/running? if not then the log indicates Foreign Key activity. So it may be that the issue is due to NULL being an invalid Foreign Key.
    • error is immediately after at androidx.room.util.TableInfoKt.readForeignKeyFieldMappings(TableInfo.kt:536)
    • kt perhaps indicates Kotlin code, for a java App????

www.un.org/Depts/DGACM/index_spanish.htm Edit Adding Additional entities

  • Add BottleEntity, no issues after run.
  • Add BottleIngredientEntity, requires IngredientTypeEntity.
  • Add IngredientTypeEntity NPE cursor.getString(toColumnIndex) must not be null

www.un.org/Depts/DGACM/index_spanish.htm 因此,与移民/移民群体有关。

  • commented out FKEY definitions, issue remains.
  • commented out IngredientTYpeEntity, issue remains.
  • commented out BottleIngredientEntity, re-introduced IngredientTypeEntity, no issues after run.

因此,这个问题似乎很多见BottleIngredientEntity

- note uninstalling App prior to each run.




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签