这是我的密码
class BookmarkViewModel(app: Application) : AndroidViewModel(app) {
private val dao = BookmarkDb.get(app).bookmarkDao()
companion object {
private const val PAGE_SIZE = PagingConstants.PERPAGE
/**
* If placeholders are enabled, PagedList will report the full size but some items might
* be null in onBind method (PagedListAdapter triggers a rebind when data is loaded).
* <p>
* If placeholders are disabled, onBind will never receive null but as more pages are
* loaded, the scrollbars will jitter as new pages are loaded. You should probably disable
* scrollbars if you disable placeholders.
*/
private const val ENABLE_PLACEHOLDERS = true
}
val allBookmarks = LivePagedListBuilder(dao.allBookmarkByDatetime(), PagedList.Config.Builder()
.setPageSize(PAGE_SIZE)
.setEnablePlaceholders(ENABLE_PLACEHOLDERS)
.build()).build()
fun insert(title: String, description: String, datetime: String) = ioThread {
dao.insert(Bookmark(id = 0, title = title, description = description, datetime = datetime))
}
fun remove(bookmark: Bookmark) = ioThread {
dao.delete(bookmark)
}
}
这是谷歌的样本。。在我想:
class BookmarkListFragment : FirstFragment() {
private var viewModel: BookmarkViewModel? = null
..
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(activity!!).get(BookmarkViewModel::class.java)
...
问题是:
java.lang.runtimeexception:无法创建com.lacas.db.room.bookmarkviewmodel类的示例
我能把这个用在片段里吗?
java.lang.runtimeexception:无法启动activity componentinfo{com.lacas.asd/com.lacas.asd.ui.activities.testtabs.testtab2activity}:java.lang.runtimeexception:无法在android.app.activitythread.performlaunchactivity(activitythread)上创建com.lacas.asd.db.room.bookmarkviewmodel类的示例。java:2924)在android.app.activitythread.handlelaunchactivity(活动线程。java:2985)在android.app.activitythread.-wrap14(activitythread.java)在android.app.activitythread$h.handlemessage(activitythread.java)。java:1635)在android.os.handler.dispatchmessage(handler。java:102)在android.os.looper.loop(looper。java:154)在android.app.activitythread.main(活动线程。java:6692)java.lang.reflect.method.invoke(本机方法)com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit)。java:1468)在com.android.internal.os.zygoteinit.main(zygoteinit。java:1358)原因:java.lang.runtimeexception:无法创建类的示例com.lacas.asd.db.room.bookmarkviewmodel位于android.arch.lifecycle.viewmodelprovider$androidviewmodelfactory.create(viewmodelprovider)。java:207)在android.arch.lifecycle.viewmodelprovider.get(viewmodelprovider。java:134)在android.arch.lifecycle.viewmodelprovider.get(viewmodelprovider。java:102)在com.lacas.asd.ui.activities.testtabs.testtab2activity$viewmodel$2.invoke(testtab2activity.kt:34)在com.lacas.asd.ui.activities.testtabs.testtab2activity$viewmodel$2.invoke(testtab2activity.kt:29)在kotlin.unsafelazyimpl.getvalue(lazy.kt:154)在com.lacas.asd.ui.activities.testtabs.testtab2activity.getviewmodel(testtab2activity.kt)在com.lacas.asd.ui.activities.testtabs.testtab2activity.oncreated(testtab2activity.kt:45),位于com.lacas.asd.base.basepermissionsactivity.oncreate(basepermissionsactivity.kt:34),位于android.app.activity.performcreate(activity)。java:6912)在android.app.instrumentation.callactivityoncreate(instrumentation。java:1126)在android.app.activitythread.performlaunchactivity(活动线程。java:2877)在android.app.activitythread.handlelaunchactivity(activitythread。java:2985) 在android.app.activitythread.-wrap14(activitythread.java) 在android.app.activitythread$h.handlemessage上。java:1635) 在android.os.handler.dispatchmessage(handler。java:102) 在android.os.looper.loop(looper。java:154) 在android.app.activitythread.main(activitythread。java:6692) 位于java.lang.reflect.method.invoke(本机方法) 在com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit。java:1468) 在com.android.internal.os.zygoteinit.main(zygoteinit。java:1358) 原因:java.lang.reflect.invocationtargetexception at java.lang.reflect.constructor.newinstance0(本机方法)at java.lang.reflect.constructor.newinstance(构造函数)。java:430)在android.arch.lifecycle.viewmodelprovider$androidviewmodelfactory.create(viewmodelprovider。java:199)在android.arch.lifecycle.viewmodelprovider.get(viewmodelprovider。java:134) 在android.arch.lifecycle.viewmodelprovider.get(viewmodelprovider。java:102) 位于com.lacas.asd.ui.activities.testtabs.testtab2activity$viewmodel$2.invoke(testtab2activity.kt:34) 位于com.lacas.asd.ui.activities.testtabs.testtab2activity$viewmodel$2.invoke(testtab2activity.kt:29) 在kotlin.unsafelazyimpl.getvalue(lazy.kt:154) 位于com.lacas.asd.ui.activities.testtabs.testtab2activity.getviewmodel(testtab2activity.kt) 位于com.lacas.asd.ui.activities.testtabs.testtab2activity.oncreated(testtab2activity.kt:45) 位于com.lacas.asd.base.basepermissionsactivity.oncreate(basepermissionsactivity.kt:34) 在android.app.activity.performcreate(activity。java:6912) 在android.app.instrumentation.callactivityoncreate(instrumentation。java:1126) 在android.app.activitythread.performlaunchactivity(activitythread。java:2877) 在android.app.activitythread.handlelaunchactivity(activitythread。java:2985) 在android.app.activitythread.-wrap14(activitythread.java) 在android.app.activitythread$h.handlemessage上。java:1635) 在android.os.handler.dispatchmessage(handler。java:102) 在android.os.looper.loop(looper。java:154) 在android.app.activitythread.main(activitythread。java:6692) 位于java.lang.reflect.method.invoke(本机方法) 在com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit。java:1468) 在com.android.internal.os.zygoteinit.main(zygoteinit。java:1358) 原因:java.lang.runtimeexception:找不到com.lacas.asd.db.room.bookmarkdb的实现。android.arch.persistence.room.room.getGenerateImplementation(room)中不存在bookmarkdb\u impl。java:93)在android.arch.persistence.room.roomdatabase$builder.build(roomdatabase。java:630)在com.lacas.asd.db.room.bookmarkdb$companion.get(bookmarkdb.kt:29)在com.lacas.asd.db.room.bookmarkviewmodel.(bookmarkviewmodel.kt:14)在java.lang.reflect.constructor.newinstance0(本机方法) 在java.lang.reflect.constructor.newinstance(constructor。java:430) 在android.arch.lifecycle.viewmodelprovider$androidviewmodelfactory.create(viewmodelprovider。java:199) 在android.arch.lifecycle.viewmodelprovider.get(viewmodelprovider。java:134) 在android.arch.lifecycle.viewmodelprovider.get(viewmodelprovider。java:102) 位于com.lacas.asd.ui.activities.testtabs.testtab2activity$viewmodel$2.invoke(testtab2activity.kt:34) 位于com.lacas.asd.ui.activities.testtabs.testtab2activity$viewmodel$2.invoke(testtab2activity.kt:29) 在kotlin.unsafelazyimpl.getvalue(lazy.kt:154) 位于com.lacas.asd.ui.activities.testtabs.testtab2activity.getviewmodel(testtab2activity.kt) 位于com.lacas.asd.ui.activities.testtabs.testtab2activity.oncreated(testtab2activity.kt:45) 位于com.lacas.asd.base.basepermissionsactivity.oncreate(basepermissionsactivity.kt:34) 在android.app.activity.performcreate(activity。java:6912) 在android.app.instrumentation.callactivityoncreate(instrumentation。java:1126) 在android.app.activitythread.performlaunchactivity(activitythread。java:2877) 在android.app.activitythread.handlelaunchactivity(activitythread。java:2985) 在android.app.activitythread.-wrap14(activitythread.java) 在android.app.activitythread$h.handlemessage上。java:1635) 在android.os.handler.dispatchmessage(handler。java:102) 在android.os.looper.loop(looper。java:154) 在android.app.activitythread.main(activitythread。java:6692) 位于java.lang.reflect.method.invoke(本机方法) 在com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit。java:1468) 在com.android.internal.os.zygoteinit.main(zygoteinit。java:1358)
7条答案
按热度按时间o4hqfura1#
有人说:
android room persistent:appdatabase\u impl不存在
解决方案是:
xa9qqrwz2#
改变
viewModel = ViewModelProviders.of(activity!!).get(BookmarkViewModel::class.java)
至viewModel = ViewModelProviders.of(this).get(BookmarkViewModel::class.java)
此外,不要示例化viewModel
至null
. 把它改成lateinit var
这样您就不必立即示例化(但是您告诉kotlin您将在访问它之前示例化它)。你可以这样做:private lateinit var viewModel: BookMarkViewModel
问题的根源是Room Dependencies
如果不是在同一版本上,或者annotationProcessor
被用来代替kapt
(kapt
使用kotlin时需要)zdwk9cvp3#
删除kapt“x..”,如果你仍然在gradle.build中使用它,因为它已经被弃用并添加了
在应用程序模块的gradle.build的末尾。这解决了我在androidstudio3.1中的问题
5anewei64#
这三件事对我很有用:
在依赖项中添加/保留annotationprocessor和kapt
annotationProcessor "android.arch.lifecycle:compiler:$lifecycle_version" kapt "android.arch.lifecycle:compiler:$lifecycle_version"
annotationProcessor "android.arch.persistence.room:compiler:$room_version" kapt "android.arch.persistence.room:compiler:$room_version"
添加apply plugin: 'kotlin-kapt'
在build.gradle(app)和清理项目的顶部重新安装应用程序
rdrgkggo5#
在我的情况下,我被添加
private set
在databaseclass中:|我删除了它。收件人:
这个问题花了我2个小时:|||
fykwrbwg6#
在我的例子中,我忘了用
ioekq8ef7#
在我的例子中,这个实现解决了我的问题
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
在我实现这个之前implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"