我的app数据库类

@Database(entities = {Detail.class}, version = Constant.DATABASE_VERSION)
public abstract class AppDatabase extends RoomDatabase {

    private static AppDatabase INSTANCE;

    public abstract FavoritesDao favoritesDao();

    public static AppDatabase getAppDatabase(Context context) {
        if (INSTANCE == null) {
            INSTANCE =
                    Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, Constant.DATABASE).allowMainThreadQueries().build();

                    //Room.inMemoryDatabaseBuilder(context.getApplicationContext(),AppDatabase.class).allowMainThreadQueries().build();
        }
        return INSTANCE;
    }

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

Gradle自由:

 compile "android.arch.persistence.room:runtime:+"   
 annotationProcessor "android.arch.persistence.room:compiler:+"

当我请求实例时,它会给出这个错误,AppDatabase_Impl不存在 在我的应用课上

public class APp extends Application {

    private boolean appRunning = false;

    @Override
    public void onCreate() {
        super.onCreate();
        AppDatabase.getAppDatabase(this); //--AppDatabase_Impl does not exist

    }   

}

当前回答

使用下面的gradle链接:

compile 'android.arch.persistence.room:runtime:1.0.0-alpha9'
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0-alpha9'

你需要创建一个不同的单例类,并从那里获取AppDatabase,如下所示:

RoomDB.java

public class RoomDB {

private static RoomDB INSTANCE;

public static AppDatabase getInstance(Context context) {
    if (INSTANCE == null) {
        INSTANCE =
                Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, Constant.DATABASE).allowMainThreadQueries().build();

                //Room.inMemoryDatabaseBuilder(context.getApplicationContext(),AppDatabase.class).allowMainThreadQueries().build();
    }
    return INSTANCE;
}

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

App.java

public class App extends Application {

private boolean appRunning = false;

@Override
public void onCreate() {
    super.onCreate();
    RoomDB.getInstance(this); //This will provide AppDatabase Instance
}

其他回答

如果您正在使用kotlin类来实现数据库,那么 使用

apply plugin: 'kotlin-kapt'

and

kapt "android.arch.persistence.room:compiler:1.1.1"

在您的gradle文件中,它将工作。

使用下面的gradle链接:

compile 'android.arch.persistence.room:runtime:1.0.0-alpha9'
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0-alpha9'

你需要创建一个不同的单例类,并从那里获取AppDatabase,如下所示:

RoomDB.java

public class RoomDB {

private static RoomDB INSTANCE;

public static AppDatabase getInstance(Context context) {
    if (INSTANCE == null) {
        INSTANCE =
                Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, Constant.DATABASE).allowMainThreadQueries().build();

                //Room.inMemoryDatabaseBuilder(context.getApplicationContext(),AppDatabase.class).allowMainThreadQueries().build();
    }
    return INSTANCE;
}

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

App.java

public class App extends Application {

private boolean appRunning = false;

@Override
public void onCreate() {
    super.onCreate();
    RoomDB.getInstance(this); //This will provide AppDatabase Instance
}

在2023年1月,我在重构我的代码以使用ServiceLocator类后遇到了类似的问题。

为了解决这个问题,我在更衣室里玩了很多次。它适用于2.5.0-alpha02

Version_room = "2.5.0-alpha02" <——build。gradle(项目)

 //Room
implementation "androidx.room:room-runtime:$version_room"
kapt "androidx.room:room-compiler:$version_room"
// optional - Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$version_room"
implementation("androidx.room:room-guava:$version_room")

我在Android Eel 2020.1.1上运行以下程序:

Version_kotlin = "1.7.21" Version_android_gradle_plugin = "4.0.1"

同意以上回答

解决方法如下。如下所示,将annotationProcessor更改为kapt

// annotationProcessor "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"

针对Kotlin开发人员

用这个:

implementation "android.arch.persistence.room:runtime:1.0.0"
kapt "android.arch.persistence.room:compiler:1.0.0"

并添加apply plugin: 'kotlin-kapt'到应用级别build.gradle的顶部。

Java开发人员须知

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