NoSuchMethodException: <init>[класс android.app.Application] - PullRequest
0 голосов
/ 14 октября 2018

Я пытаюсь переключить мое приложение с Loader с на Room и 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()

}

In MainActivity.onCreate() Я инициализирую свой TimetableViewModel следующей строкой:

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

Но при запуске приложение вылетает с журналом:

Caused by: java.lang.RuntimeException: Cannot create an instance of class com.contedevel.timetable.model.TimetableViewModel
        at android.arch.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:201)
        at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:134)
        at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:102)
        at com.contedevel.timetable.activity.MainActivity.onCreate(MainActivity.kt:187)
        at android.app.Activity.performCreate(Activity.java:6757)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2702)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2810) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6312) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 
     Caused by: java.lang.NoSuchMethodException: <init> [class android.app.Application]
        at java.lang.Class.getConstructor0(Class.java:2204)
        at java.lang.Class.getConstructor(Class.java:1683)
        at android.arch.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:199)
        at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:134) 
        at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:102) 
        at com.contedevel.timetable.activity.MainActivity.onCreate(MainActivity.kt:187) 
        at android.app.Activity.performCreate(Activity.java:6757) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2702) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2810) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6312) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
...