为什么Room告诉我AppDatabase_Impl不存在错误?

时间:2018-01-17 09:16:09

标签: android android-room

  

01-17 14:25:21.567 5084-5084 /? E / AndroidRuntime:致命异常:主要                                                    处理:com.example.harv.swat,PID:5084                                                    java.lang.RuntimeException:无法启动活动   ComponentInfo {com.example.harv.swat / com.example.harv.swat.HomeActivity}:   java.lang.RuntimeException:无法找到实现   com.example.harv.swat.model.AppDatabase。 AppDatabase_Impl没有   存在                                                        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2696)                                                        在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2757)                                                        在android.app.ActivityThread.-wrap12(ActivityThread.java)                                                        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1504)                                                        在android.os.Handler.dispatchMessage(Handler.java:102)                                                        在android.os.Looper.loop(Looper.java:163)                                                        在android.app.ActivityThread.main(ActivityThread.java:6237)                                                        at java.lang.reflect.Method.invoke(Native Method)                                                        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:877)                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)                                                     引起:java.lang.RuntimeException:无法找到实现   com.example.harv.swat.model.AppDatabase。 AppDatabase_Impl没有   存在                                                        在android.arch.persistence.room.Room.getGeneratedImplementation(Room.java:92)                                                        在android.arch.persistence.room.RoomDatabase $ Builder.build(RoomDatabase.java:454)                                                        在com.example.harv.swat.model.Db。(Db.java:16)                                                        在com.example.harv.swat.model.Db.getInstance(Db.java:20)                                                        在com.example.harv.swat.HomeActivity.onCreate(HomeActivity.java:28)                                                        在android.app.Activity.performCreate(Activity.java:6847)                                                        在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)                                                        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2649)                                                        在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2757)                                                        在android.app.ActivityThread.-wrap12(ActivityThread.java)                                                        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1504)                                                        在android.os.Handler.dispatchMessage(Handler.java:102)                                                        在android.os.Looper.loop(Looper.java:163)                                                        在android.app.ActivityThread.main(ActivityThread.java:6237)                                                        at java.lang.reflect.Method.invoke(Native Method)                                                        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:877)                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

public class Db {
AppDatabase adb;
private static Db db;

private Db(Context context){
    adb = Room.databaseBuilder(context,
            AppDatabase.class, "swat").allowMainThreadQueries().build();
}
public static Db getInstance(Context context){
    if(db==null){
        db = new Db(context);
    }
    return db;
}
public MyDAO getDao(){
    return adb.userDao();
}}

DOA课程

@Dao
public interface MyDAO {
@Query("SELECT * FROM content")
List<Content> getAll();

@Query("SELECT * FROM content WHERE id IN (:userIds)")
List<Content> loadAllByIds(int[] userIds);

/*@Query("SELECT * FROM user WHERE first_name LIKE :first AND "
        + "last_name LIKE :last LIMIT 1")
Content findByName(String first, String last);*/

@Insert
void insertAll(Content... users);

@Delete
void delete(Content user);}

Gradle文件

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:1.0.0"
implementation "android.arch.lifecycle:common-java8:1.0.0"
//annotationProcessor "android.arch.lifecycle:compiler:1.0.0"

// Room
implementation "android.arch.persistence.room:runtime:1.0.0"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"

// Test helpers for Room
testImplementation "android.arch.persistence.room:testing:1.0.0"
}

1 个答案:

答案 0 :(得分:1)

更改时,可能无法重新生成文件。也许尝试重建项目:Build - &gt;重建