如何读取和写入房间android

时间:2018-02-20 06:52:15

标签: android android-room greendao

我尝试使用带有greenDao 2的房间库来安装我的Android应用程序,但它因此类错误而崩溃。

 Process: com.example.hp_pk.dictionary, PID: 3996
                                                                            java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                                at android.os.AsyncTask$3.done(AsyncTask.java:353)
                                                                                at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
                                                                                at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
                                                                                at java.util.concurrent.FutureTask.run(FutureTask.java:271)
                                                                                at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
                                                                                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
                                                                                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
                                                                                at java.lang.Thread.run(Thread.java:764)
                                                                             Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/arch/core/executor/AppToolkitTaskExecutor;
                                                                                at android.arch.persistence.room.RoomDatabase.assertNotMainThread(RoomDatabase.java:138)
                                                                                at android.arch.persistence.room.RoomDatabase.query(RoomDatabase.java:167)
                                                                                at com.example.hp_pk.dictionary.room_database.dao.SubjectDao_Impl.getAll(SubjectDao_Impl.java:69)
                                                                                at com.example.hp_pk.dictionary.room_database.data_source.SubjectDataSource.getAll(SubjectDataSource.java:32)
                                                                                at com.example.hp_pk.dictionary.presentation.presenter.MyTutorPresenter$DataBaseAsyncGet.doInBackground(MyTutorPresenter.java:56)
                                                                                at com.example.hp_pk.dictionary.presentation.presenter.MyTutorPresenter$DataBaseAsyncGet.doInBackground(MyTutorPresenter.java:52)
                                                                                at android.os.AsyncTask$2.call(AsyncTask.java:333)
                                                                                at java.util.concurrent.FutureTask.run(FutureTask.java:266)
                                                                                at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) 
                                                                                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 
                                                                                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 
                                                                                at java.lang.Thread.run(Thread.java:764) 
                                                                             Caused by: java.lang.ClassNotFoundException: Didn't find class "android.arch.core.executor.AppToolkitTaskExecutor" on path: DexPathList[[zip file "/data/app/com.example.hp_pk.dictionary-ZogyaA9ar13xwY5j7uX5Tg==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.hp_pk.dictionary-ZogyaA9ar13xwY5j7uX5Tg==/lib/x86, /data/app/com.example.hp_pk.dictionary-ZogyaA9ar13xwY5j7uX5Tg==/base.apk!/lib/x86, /system/lib, /vendor/lib]]
                                                                                at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
                                                                                at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
                                                                                at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
                                                                                at android.arch.persistence.room.RoomDatabase.assertNotMainThread(RoomDatabase.java:138) 
                                                                                at android.arch.persistence.room.RoomDatabase.query(RoomDatabase.java:167) 
                                                                                at com.example.hp_pk.dictionary.room_database.dao.SubjectDao_Impl.getAll(SubjectDao_Impl.java:69) 
                                                                                at com.example.hp_pk.dictionary.room_database.data_source.SubjectDataSource.getAll(SubjectDataSource.java:32) 
                                                                                at com.example.hp_pk.dictionary.presentation.presenter.MyTutorPresenter$DataBaseAsyncGet.doInBackground(MyTutorPresenter.java:56) 
                                                                                at com.example.hp_pk.dictionary.presentation.presenter.MyTutorPresenter$DataBaseAsyncGet.doInBackground(MyTutorPresenter.java:52) 
                                                                                at android.os.AsyncTask$2.call(AsyncTask.java:333) 
                                                                                at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
                                                                                at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) 
                                                                                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 
                                                                                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 
                                                                                at java.lang.Thread.run(Thread.java:764) 

这是我的DatabaseModule

@Singleton
@Module
public class DatabaseModule {

    private AppDatabase database;

    @Singleton
    @Provides
    AppDatabase provideRoomDatabase(Context context) {
        database = Room.databaseBuilder(context, AppDatabase.class, "room_db").build();
        return database;
    }

    @Singleton
    @Provides
    SubjectDao provideSubjectDao(AppDatabase database) {
        return database.subjectDao();
    }

    @Singleton
    @Provides
    SubjectRepository getSubjectRepository(SubjectDao subjectDao) {
        return new SubjectDataSource(subjectDao);
    }

}

和SubjectDataSource

public class SubjectDataSource implements SubjectRepository {
    private SubjectDao subjectDao;

    public SubjectDataSource(SubjectDao subjectDao) {
        this.subjectDao = subjectDao;
    }
    @Override
    public void insertAll(List<Subjects> list) {
        subjectDao.insertAll(list);
    }

    @Override
    public void insert(Subjects subjects) {
        subjectDao.insert(subjects);
    }

    @Override
    public List<Subjects> getAll() {
        return subjectDao.getAll();
    }
}

android SubjectDao

@Dao
public interface SubjectDao {
    @Query("select * from subjects")
    List<Subjects> getAll();

    @Query("select * from subjects where id ==:id")
    Subjects getSubject(int id);

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertAll(List<Subjects> subjects);

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insert(Subjects subjects);
}

我尝试插入并阅读MyTutorRepasitory

@InjectViewState
public class MyTutorPresenter extends MvpPresenter<MyTutorView> {

    private MyTutorView stateView;

    @Inject
    SubjectRepository subjectRepository;

    public MyTutorPresenter() {
        Dictionary.getAppComponent().inject(this);
        stateView = getViewState();
    }

    private void showAllSubjectsInUI() {
        new DataBaseAsyncGet().execute();
    }

    public void setAllSubjects(List<Subjects> list) {
        new DataBaseAsync().execute(list);
        showAllSubjectsInUI();
    }

    private class DataBaseAsync extends AsyncTask<List<Subjects>, Void, Void> {

        @Override
        protected Void doInBackground(List<Subjects>[] lists) {
            subjectRepository.insertAll(lists[0]);
            return null;
        }
    }

    private class DataBaseAsyncGet extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... voids) {
            stateView.setAllSubjects(subjectRepository.getAll());
            return null;
        }
    }

}

0 个答案:

没有答案