会议室未创建数据库

时间:2019-11-12 12:00:30

标签: android android-room

我正在使用Room为我的应用创建一个小的数据库。我知道当我使用DAO类中的某些函数时应该创建一个数据库,但是无法正常工作。

我正在预填充数据库,因此自从安装该应用程序以来,应该在数据库中保存数据,并且我一直在尝试在这里找到的所有内容,并且确定自己犯了一些新手愚蠢的错误。

Video.class

@Entity(tableName = "videos_table")
public class Video {

    @PrimaryKey(autoGenerate = true)
    private int id;
    private String video;

    public Video(String video) {
        this.video = video;
    }

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

    public int getId() { return id; }

    public String getVideo() { return video; }
}

VideoDatabase.class

@Database(entities = Video.class, version = 1)
public abstract class VideoDatabase extends RoomDatabase {

    private static VideoDatabase instance;
    public abstract VideoDAO videoDAO();

    public static synchronized VideoDatabase getInstance(Context context) {
        if (instance == null) {
            instance = Room.databaseBuilder(context.getApplicationContext(),
                    VideoDatabase.class, "note_database")
                    .fallbackToDestructiveMigration()
                    .addCallback(roomCallback)
                    .build();
        }

        return instance;
    }

    private static RoomDatabase.Callback roomCallback = new RoomDatabase.Callback() {

        @Override
        public void onCreate(@NonNull SupportSQLiteDatabase db) {
            super.onCreate(db);
            new PopulatedDbAsyncTask(instance).execute();
        }
    };

    private static class PopulatedDbAsyncTask extends AsyncTask<Void, Void, Void> {

        private VideoDAO videoDAO;
        private PopulatedDbAsyncTask(VideoDatabase db) {
            videoDAO = db.videoDAO();
        }

        @Override
        protected Void doInBackground(Void... voids) {
            videoDAO.insert(new Video("Title 1"));
            videoDAO.insert(new Video("Title 2"));
            videoDAO.insert(new Video("Title 3"));
            return null;
        }
    }
}

VideoDAO

@Dao
public interface VideoDAO {

    @Insert
    void insert(Video video);

    @Update
    void update(Video video);

    @Delete
    void delete(Video video);

    @Query("DELETE FROM videos_table")
    void deteleAllVideos();


}

VideoRepository

public class VideoRepository {

    private VideoDAO videoDAO;
    private LiveData<List<Video>> allNotes;

    public VideoRepository(Application application) {
        VideoDatabase database = VideoDatabase.getInstance(application);
        videoDAO = database.videoDAO();
        allNotes = videoDAO.getAllVideos();
    }

    public void insert(Video note) {

        new InsertNoteAsyncTask(videoDAO).execute(note);
    }

    public void update(Video note) {

        new UpdateNoteAsyncTask(videoDAO).execute(note);
    }

    public void delete(Video note) {

        new DeleteNoteAsyncTask(videoDAO).execute(note);
    }

    public void deleteAllNotes() {

        new DeleteAllNotesAsyncTask(videoDAO).execute();
    }

    public LiveData<List<Video>> getAllNotes() {
        return allNotes;
    }



    private static class InsertNoteAsyncTask extends AsyncTask<Video, Void, Void> {

        private VideoDAO videoDAO;

        private InsertNoteAsyncTask(VideoDAO videoDAO) {
            this.videoDAO = videoDAO;
        }

        @Override
        protected Void doInBackground(Video... notes) {
            videoDAO.insert(notes[0]);
            return null;
        }
    }

    private static class UpdateNoteAsyncTask extends AsyncTask<Video, Void, Void> {

        private VideoDAO videoDAO;

        private UpdateNoteAsyncTask(VideoDAO videoDAO) {
            this.videoDAO = videoDAO;
        }

        @Override
        protected Void doInBackground(Video... notes) {
            videoDAO.update(notes[0]);
            return null;
        }
    }

    private static class DeleteNoteAsyncTask extends AsyncTask<Video, Void, Void> {

        private VideoDAO videoDAO;

        private DeleteNoteAsyncTask(VideoDAO videoDAO) {
            this.videoDAO = videoDAO;
        }

        @Override
        protected Void doInBackground(Video... notes) {
            videoDAO.delete(notes[0]);
            return null;
        }
    }

    private static class DeleteAllNotesAsyncTask extends AsyncTask<Void, Void, Void> {

        private VideoDAO videoDAO;

        private DeleteAllNotesAsyncTask(VideoDAO videoDAO) {
            this.videoDAO = videoDAO;
        }

        @Override
        protected Void doInBackground(Void... voids) {
            videoDAO.deteleAllVideos();
            return null;
        }
    }
}

这是logcat

2019-11-12 13:26:49.207 11006-11006/? I/zygote: Not late-enabling -Xcheck:jni (already on)
2019-11-12 13:26:49.258 11006-11006/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
2019-11-12 13:26:49.305 11006-11021/? E/zygote: Failed sending reply to debugger: Broken pipe
2019-11-12 13:26:49.305 11006-11021/? I/zygote: Debugger is no longer active
2019-11-12 13:26:49.636 11006-11006/? I/zygote: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener;
2019-11-12 13:26:49.637 11006-11006/? I/zygote:     at void androidx.core.view.ViewCompat.setBackground(android.view.View, android.graphics.drawable.Drawable) (ViewCompat.java:2559)
2019-11-12 13:26:49.637 11006-11006/? I/zygote:     at void androidx.appcompat.widget.ActionBarContainer.<init>(android.content.Context, android.util.AttributeSet) (ActionBarContainer.java:63)
2019-11-12 13:26:49.637 11006-11006/? I/zygote:     at java.lang.Object java.lang.reflect.Constructor.newInstance0(java.lang.Object[]) (Constructor.java:-2)
2019-11-12 13:26:49.637 11006-11006/? I/zygote:     at java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[]) (Constructor.java:334)
2019-11-12 13:26:49.637 11006-11006/? I/zygote:     at android.view.View android.view.LayoutInflater.createView(java.lang.String, java.lang.String, android.util.AttributeSet) (LayoutInflater.java:647)
2019-11-12 13:26:49.637 11006-11006/? I/zygote:     at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:790)
2019-11-12 13:26:49.637 11006-11006/? I/zygote:     at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet) (LayoutInflater.java:730)
2019-11-12 13:26:49.637 11006-11006/? I/zygote:     at void android.view.LayoutInflater.rInflate(org.xmlpull.v1.XmlPullParser, android.view.View, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:863)
2019-11-12 13:26:49.637 11006-11006/? I/zygote:     at void android.view.LayoutInflater.rInflateChildren(org.xmlpull.v1.XmlPullParser, android.view.View, android.util.AttributeSet, boolean) (LayoutInflater.java:824)
2019-11-12 13:26:49.637 11006-11006/? I/zygote:     at android.view.View android.view.LayoutInflater.inflate(org.xmlpull.v1.XmlPullParser, android.view.ViewGroup, boolean) (LayoutInflater.java:515)
2019-11-12 13:26:49.637 11006-11006/? I/zygote:     at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean) (LayoutInflater.java:423)
2019-11-12 13:26:49.637 11006-11006/? I/zygote:     at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup) (LayoutInflater.java:374)
2019-11-12 13:26:49.637 11006-11006/? I/zygote:     at android.view.ViewGroup androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:749)
2019-11-12 13:26:49.637 11006-11006/? I/zygote:     at void androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:659)
2019-11-12 13:26:49.637 11006-11006/? I/zygote:     at void androidx.appcompat.app.AppCompatDelegateImpl.setContentView(int) (AppCompatDelegateImpl.java:552)
2019-11-12 13:26:49.637 11006-11006/? I/zygote:     at void androidx.appcompat.app.AppCompatActivity.setContentView(int) (AppCompatActivity.java:161)
2019-11-12 13:26:49.637 11006-11006/? I/zygote:     at void com.example.recordshakerservice.MainActivity.onCreate(android.os.Bundle) (MainActivity.java:34)
2019-11-12 13:26:49.637 11006-11006/? I/zygote:     at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:6975)
2019-11-12 13:26:49.637 11006-11006/? I/zygote:     at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1213)
2019-11-12 13:26:49.637 11006-11006/? I/zygote:     at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2770)
2019-11-12 13:26:49.638 11006-11006/? I/zygote:     at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:2892)
2019-11-12 13:26:49.638 11006-11006/? I/zygote:     at void android.app.ActivityThread.-wrap11(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
2019-11-12 13:26:49.638 11006-11006/? I/zygote:     at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1593)
2019-11-12 13:26:49.638 11006-11006/? I/zygote:     at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:105)
2019-11-12 13:26:49.638 11006-11006/? I/zygote:     at void android.os.Looper.loop() (Looper.java:164)
2019-11-12 13:26:49.638 11006-11006/? I/zygote:     at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6541)
2019-11-12 13:26:49.638 11006-11006/? I/zygote:     at java.lang.Object java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object[]) (Method.java:-2)
2019-11-12 13:26:49.638 11006-11006/? I/zygote:     at void com.android.internal.os.Zygote$MethodAndArgsCaller.run() (Zygote.java:240)
2019-11-12 13:26:49.638 11006-11006/? I/zygote:     at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:767)

任何帮助都会很棒,谢谢!

1 个答案:

答案 0 :(得分:0)

我认为您的build.gradle中没有房间注释处理器:

implementation androidx.room:room-runtime:2.2.1
//or annotationprocessor
kapt androidx.room:room-compiler:2.2.1 //make sure to apply 'kotlin-kapt' as plugin

最后一个(启用增量ap):

defaultConfig {
       ...
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = roomIncrementalAnnotationProcessor
            }
        }
    }