NoSuchMethodException:<init> [类android.app.Application]

时间:2018-10-13 22:44:57

标签: android kotlin android-lifecycle android-room

我正在尝试将我的应用程序从mutate切换到LoaderRoom s。我已经编写了自己的ViewModel类:

App

我的视图模型:

class App : Application() {

    lateinit var database: AppDatabase
        private set

    override fun onCreate() {
        super.onCreate()
        instance = this
        database = Room.databaseBuilder(this, AppDatabase::class.java, AppDatabase.NAME)
                .build()
    }

    companion object {
        lateinit var instance : App
    }
}

open class BaseViewModel<TEntity, TDao: BaseDao<TEntity>, TRepo : Repository<TEntity, TDao>>( app: App, protected val repository: TRepo) : AndroidViewModel(app) { fun insert(entity: TEntity) { repository.insert(entity) } fun update(entity: TEntity) { repository.update(entity) } fun delete(entity: TEntity) { repository.delete(entity) } } class TimetableViewModel(app: App) : BaseViewModel<Timetable, TimetableDao, TimetableRepository>( app, TimetableRepository(app.database)) { internal val all: LiveData<List<Timetable>> = repository.getAll() } 中,我的MainActivity.onCreate()由下一行初始化:

TimetableViewModel

但是在启动时,应用程序崩溃并显示日志:

timetableVM = ViewModelProviders.of(this).get(TimetableViewModel::class.java)

0 个答案:

没有答案