以下代码行崩溃
Room.databaseBuilder(
applicationContext,
MyDatabase::class.java, "MyDB")
.build()
这是您可以轻松复制的“最小完整可验证(MCV)”应用程序
在项目的build.gradle中,确保存在以下内容
allprojects {
repositories {
google()
jcenter()
}
}
在应用程序的build.gradle中,确保存在以下内容
dependencies {
def room_version = "1.1.1"
implementation "android.arch.persistence.room:runtime:$room_version"
kapt "android.arch.persistence.room:compiler:$room_version" // because I'm using Kotlin
}
所有设置说明均根据官方文档https://developer.android.com/topic/libraries/architecture/adding-components
现在只需使用Android Studio并使用“空活动”模板创建项目。在此MCV示例中,仅需要3个新文件。
创建名为MyThing.kt
//MyThing.kt
import android.arch.persistence.room.ColumnInfo
import android.arch.persistence.room.Entity
@Entity(tableName = "myThing")
data class MyThing(@ColumnInfo var myString: String)
创建另一个名为MyDao.kt
的文件
import android.arch.persistence.room.Dao
import android.arch.persistence.room.Insert
import android.arch.persistence.room.OnConflictStrategy
@Dao
interface MyDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(myThing: MyThing)
}
创建名为MyDatabase.kt
的第三个文件
import android.arch.persistence.room.Database
import android.arch.persistence.room.RoomDatabase
@Database(entities = arrayOf(MyThing::class), version = 1)
abstract class MyDatabase : RoomDatabase() {
abstract fun myDao(): MyDao
}
现在修改MainActivity
类
class MainActivity : AppCompatActivity() {
private var myDB: MyDatabase? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
myDB = Room.databaseBuilder(
applicationContext,
MyDatabase::class.java, "MyDB")
.build()
}
}
现在运行该应用程序将导致我上面提到的那行严重崩溃。我在做什么错了?
更新:Logcat输出
--------- beginning of crash
12-29 13:23:46.098 12801-12801/com.le.kevin.roommcv E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.le.kevin.roommcv, PID: 12801
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.le.kevin.roommcv/com.le.kevin.roommcv.MainActivity}: java.lang.RuntimeException: cannot find implementation for com.le.kevin.roommcv.MyDatabase. MyDatabase_Impl does not exist
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.RuntimeException: cannot find implementation for com.le.kevin.roommcv.MyDatabase. MyDatabase_Impl does not exist
at android.arch.persistence.room.Room.getGeneratedImplementation(Room.java:93)
at android.arch.persistence.room.RoomDatabase$Builder.build(RoomDatabase.java:630)
at com.le.kevin.roommcv.MainActivity.onCreate(MainActivity.kt:19)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-29 13:23:46.099 777-1936/? W/ActivityManager: Force finishing activity com.le.kevin.roommcv/.MainActivity
Impl?
答案 0 :(得分:1)
我通过添加
使它起作用apply plugin: 'kotlin-kapt'
在应用的build.graddle中
我找到了答案,这要看上面评论中建议的logcat,然后再看看答案Room + cannot find implementation DB + DB_Impl does not exist
官方文档https://developer.android.com/topic/libraries/architecture/adding-components不完整