簡體   English   中英

房間不創建數據庫

[英]Room doesn't create database

我正在使用 Room 為我的應用程序創建一個小數據庫。 我知道當我使用來自 DAO class 的一些 function 時應該創建一個數據庫,但它不工作。

我正在預填充數據庫,所以自從安裝了應用程序以來,數據庫中應該有數據,我一直在嘗試我在這里找到的所有東西,我確信我犯了一些新手愚蠢的錯誤。

視頻.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; }
}

視頻數據庫.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;
        }
    }
}

視頻DAO

@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();


}

視頻庫

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;
        }
    }
}

這是日志貓

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)

任何幫助都會很棒,謝謝!

我認為您的build.gradle中沒有 Rooms 注釋處理器:

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
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM