我正在尝试创建一个通用的 ViewModelProvider.Factory 来实例化我的 ViewModel
class BundledAppFactory(private var bundle: AppParameterBundle) : ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
Log.d("Factory", "Creating ${modelClass.canonicalName}")
Log.d("Factory", "${modelClass.canonicalName} has ${modelClass.constructors.size} constructors and ${modelClass.declaredConstructors.size} declaredConstructors")
modelClass.constructors.forEach {
Log.d("Factory", "Constructor: $it")
}
return when {
AppViewModel::class.java.isAssignableFrom(modelClass) -> modelClass.getDeclaredConstructor(AppParameterBundle::class.java).newInstance(bundle)
AppTeamUserViewModel::class.java.isAssignableFrom(modelClass) -> modelClass.getDeclaredConstructor(AppTeamUserParameterBundle::class.java).newInstance(bundle)
AppTeamUserHuntIdViewModel::class.java.isAssignableFrom(modelClass) -> modelClass.getDeclaredConstructor(AppTeamUserHuntIdParameterBundle::class.java).newInstance(bundle)
AppStringViewModel::class.java.isAssignableFrom(modelClass) -> modelClass.getDeclaredConstructor(AppStringParameterBundle::class.java).newInstance(bundle)
else -> super.create(modelClass)
}
}
}
这些是我的捆绑包:
open class AppParameterBundle(val application: Application)
class AppStringParameterBundle(app: Application, val string: String) : AppParameterBundle(app)
class AppTeamUserParameterBundle(app: Application,
val team: Team,
val user: User) :
AppParameterBundle(app)
class AppTeamUserHuntIdParameterBundle(app: Application,
val huntId: String,
val team: Team,
val user: User) : AppParameterBundle(app)
这些是我使用自定义 ViewModel 扩展的基本 ViewModel:
abstract class AppViewModel<MODEL, EVENT : Any>(bundle: AppParameterBundle) : BaseViewModel<MODEL, EVENT>(bundle)
abstract class AppTeamUserViewModel<MODEL, EVENT : Any>(bundle: AppTeamUserParameterBundle) : BaseViewModel<MODEL, EVENT>(bundle)
abstract class AppTeamUserHuntIdViewModel<MODEL, EVENT : Any>(bundle: AppTeamUserHuntIdParameterBundle) : BaseViewModel<MODEL, EVENT>(bundle)
abstract class AppStringViewModel<MODEL, EVENT : Any>(bundle: AppStringParameterBundle) : BaseViewModel<MODEL, EVENT>(bundle)
以及从其中之一扩展的 ViewModel 示例:
class TeamViewModel(bundle: AppParameterBundle) : AppViewModel<TeamUiModel, TeamEvent>(bundle) {
...
}
我的问题是
modelClass.getDeclaredConstructor(AppParameterBundle::class.java).newInstance(bundle)
不会找到任何构造函数和这条线
Log.d("Factory", "${modelClass.canonicalName} has ${modelClass.constructors.size} constructors and ${modelClass.declaredConstructors.size} declaredConstructors")
出现在日志中
mypackage.viewmodels.TeamViewModel has 0 constructors and 0 declaredConstructors
这是我在堆栈跟踪中得到的:
Caused by: java.lang.NoSuchMethodException: <init> [class mypackage.ParameterBundle]
at java.lang.Class.getConstructor(Class.java:528)
at java.lang.Class.getDeclaredConstructor(Class.java:507)
at mypackage.viewmodels.BundledAppFactory.create(FactoriesAndBundles.kt:39)
at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:128)
at mypackage.TeamActivity.onCreate(TeamActivity.kt:41)
这是没有构造函数的 Kotlin 问题吗?我该如何解决?