房间持久性AppDatabase_Impl不存在android

时间:2018-03-29 07:02:28

标签: android android-room

我已经实现了Room persistence来存储通知,但是对于builde

,它会在下面的日志中抛出异常
implementation for modules.room_db.AppDatabase. AppDatabase_Impl does not exist
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.RuntimeException: cannot find implementation for modules.room_db.AppDatabase. AppDatabase_Impl does not exist
        at android.arch.persistence.room.Room.getGeneratedImplementation(Room.java:92)
        at android.arch.persistence.room.RoomDatabase$Builder.build(RoomDatabase.java:454)
        at modules.room_db.AppDatabase.getAppDatabase(AppDatabase.java:23)
        at MypackageName.SplashActivity.onCreate(SplashActivity.java:46)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)

商店通知

AppDatabase.getAppDatabase(getApplicationContext()).userDao().insertNotification(notificationDetail);

Gralde

 implementation 'android.arch.persistence.room:runtime:1.0.0'
    annotationProcessor    'android.arch.persistence.room:compiler:1.0.0'
    androidTestImplementation 'android.arch.persistence.room:testing:1.0.0'

AppDatabase.java

public abstract class AppDatabase extends RoomDatabase {

    private static final String DATABASE_NAME = "notification_room_db";

    public abstract NotificationDao userDao();


    private static AppDatabase INSTANCE;

    public static AppDatabase getAppDatabase(Context context) {
        if (INSTANCE == null) {
            INSTANCE =
                    Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, DATABASE_NAME)
                            // allow queries on the main thread.
                            // Don't do this on a real app! See PersistenceBasicSample for an example.
                            .allowMainThreadQueries()
                            .build();
        }
        return INSTANCE;
    }

    public static void destroyInstance() {
        INSTANCE = null;
    }
}

NotificationDao.java

public interface NotificationDao {


    @Query("SELECT * FROM notifications")
    List<NotificationDetail> getAll();

    @Query("SELECT * FROM notifications WHERE notification_id IN (:userIds)")
    List<NotificationDetail> loadAllByIds(int[] userIds);


    @Insert
    void insertNotification(NotificationDetail... users);


    @Update
    void updateNotification(NotificationDetail notificationDetail);

    @Delete
    void delete(NotificationDetail user);
}

NotificationDetail.java

@Entity(tableName = "notifications")
public class NotificationDetail {

    @PrimaryKey(autoGenerate = true)
    private int notification_id;

    @ColumnInfo(name = "message")
    private String message;

    @ColumnInfo(name = "title")
    private String title;

    @ColumnInfo(name = "type")
    private String type;

    @ColumnInfo(name = "order_id")
    private int order_id;

    @ColumnInfo(name = "is_displayed")
    private boolean isSeen;

}

我已经搜索过这个问题了,kotlin也存在同样的问题我正在寻找android中的答案。

1 个答案:

答案 0 :(得分:2)

当我们使用Room persistence时,最好为每个类定义正确的注释。

1)使用注释Entity定义您的@Entity课程,如下所示

@Entity(tableName = "notifications")
public class NotificationDetail {

    @PrimaryKey(autoGenerate = true)
    private int notification_id;

    @ColumnInfo(name = "message")
    private String message;

    @ColumnInfo(name = "title")
    private String title;             
}

2)使用@Dao注释定义您的Dao界面,如下所示

@Dao
public interface NotificationDao {

    @Query("SELECT * FROM notifications")
    List<NotificationDetail> getAll();

    @Insert
    void insertNotification(NotificationDetail... users);

    @Update
    void updateNotification(NotificationDetail notificationDetail);

    @Delete
    void delete(NotificationDetail user);
}

3)扩展RoomDatabase的数据库类用所有Entity类定义@Database注释,如下所示

@Database(entities = {NotificationDetail.class}, version = 1, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {

    private static final String DATABASE_NAME = "notification_room_db";

    public abstract NotificationDao userDao();


    private static AppDatabase INSTANCE;

    public static AppDatabase getAppDatabase(Context context) {
        if (INSTANCE == null) {
            INSTANCE =
                    Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, DATABASE_NAME)
                            // allow queries on the main thread.
                            // Don't do this on a real app! See PersistenceBasicSample for an example.
                            .allowMainThreadQueries()
                            .build();
        }
        return INSTANCE;
    }

    public static void destroyInstance() {
        INSTANCE = null;
    }
}

我忘了在数据库类中添加@Database注释