Android Room主键@NonNull注解仍报错问题及相关疑问
Hey there, let's tackle your Room issues one by one, based on the details you provided:
Even though you added @NonNull to your movieId primary key, there are a few common reasons this error persists—let's break them down:
Check the field's type (critical!):
If you're using Kotlin, make suremovieIdis a non-null type (e.g.,Intinstead ofInt?). For Java, useintinstead ofInteger. Room enforces that primary keys are non-null at the type level too, so a nullable type paired with@NonNullwill still trigger this error.
Example of a correct entity class (Kotlin):import androidx.room.Entity import androidx.room.PrimaryKey import androidx.annotation.NonNull @Entity(tableName = "favorite_movies") data class FavoriteMovie( @PrimaryKey @NonNull val movieId: Int, // Non-null type here val title: String, val posterUrl: String )Verify the correct
@NonNullimport:
Ensure you're usingandroidx.annotation.NonNull(notorg.jetbrains.annotations.NonNullor Lombok's@NonNull). Room only recognizes the AndroidX variant of this annotation.Check DAO methods for consistency:
If your DAO has methods referencingmovieId(e.g.,@Query("SELECT * FROM favorite_movies WHERE movieId = :id")), make sure the parameteridis also annotated with@NonNulland uses a non-null type. Mismatches here can sometimes cause cascading errors.Update Room to a newer stable version:
You're using Room 2.2.1, which is quite outdated. Newer versions (e.g., 2.5.0+) fix many annotation processing bugs. Update yourbuild.gradledependencies:// In your module-level build.gradle ext.roomVersion = "2.5.0" dependencies { implementation "androidx.room:room-runtime:$roomVersion" kapt "androidx.room:room-compiler:$roomVersion" // For Kotlin // annotationProcessor "androidx.room:room-compiler:$roomVersion" // For Java }Clean and rebuild your project:
Sometimes cached generated code can cause false errors. Go toBuild > Clean ProjectthenBuild > Rebuild Projectin Android Studio to clear old artifacts.
No, they won't be created. Room relies on annotation processing during compilation to generate critical classes (like your AppDatabase_Impl implementation). If compilation fails, these generated classes are never produced, so your app can't initialize the database at runtime. There's no way for the database file or tables to exist in this scenario.
The location depends on whether you're using an emulator or physical device:
Emulator or rooted physical device:
The database file lives in:/data/data/[your-app-package-name]/databases/You can access it directly via Android Studio's Device File Explorer (go to
View > Tool Windows > Device File Explorer). Look for your app's package name, navigate to thedatabasesfolder, and you'll see files likeyour-db-name.dband auxiliary filesyour-db-name.db-wal/your-db-name.db-shm.Non-rooted physical device (debug build):
You can still pull the database file using Android Studio:- Open Device File Explorer
- Find your app's
databasesfolder - Right-click the
.dbfile and selectSave Asto copy it to your computer. You can then open it with tools like SQLiteStudio or DB Browser for SQLite.
Release build on non-rooted device:
You can't access the database directly, as app data is sandboxed. To inspect it, you can add debug-only code to your app to copy the database file to external storage:// Example code to copy DB to external storage (debug only) fun copyDbToExternalStorage(context: Context) { val dbPath = context.getDatabasePath("your-db-name.db").absolutePath val externalDir = context.getExternalFilesDir(null) ?: return val externalPath = "${externalDir.absolutePath}/your-db-name.db" try { File(dbPath).copyTo(File(externalPath), overwrite = true) } catch (e: IOException) { e.printStackTrace() } }This uses scoped storage (compatible with Android 10+) so you won't need extra permissions beyond what your app already has.
内容的提问来源于stack exchange,提问作者Ha Luu

