Androidviewmodel有一个App参数,异常has no zero argument constructor?

请教大佬们
kotlin继承Androidviewmodel,并声明一个Application的参数的构造方法,最后InstantiationException,has no zero argument constructor?
在AddCityActivity.kt中这一行报错:

viewModel=ViewModelProvider(this).get(SearchViewModel::class.java)

注释中改写了一个Factory可以通过,但是别人的代码没有写也可以
SearchViewModel.kt


class SearchViewModel(val app:Application):AndroidViewModel(app) {
    val topCities=MutableLiveData<ArrayList<String>>()


    fun getTopCities(){
        launch {
            val cityList=app.resources.getStringArray(R.array.top_city).toList() as ArrayList<String>
            topCities.postValue(cityList)
        }
    }
    fun launch(block:CoroutineScope.()->Unit){
        launchRequet(block)
    }
    fun launchRequet(block:CoroutineScope.()->Unit){
        viewModelScope.launch {
            withContext(Dispatchers.IO){
                block.invoke(this)
            }
        }
    }
}

AddCityActivity.kt

class AddCityActivity:AppCompatActivity() {
    lateinit var mBinding:ActivityAddCityBinding
    lateinit var viewModel: SearchViewModel
     val topCities by lazy { ArrayList<String>() }
    lateinit var topCitiesAdapter:TopCityAdapter
    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding= ActivityAddCityBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
        topCitiesAdapter=TopCityAdapter(topCities,{})
        mBinding.rvTopCities.adapter=topCitiesAdapter
        mBinding.rvTopCities.layoutManager=GridLayoutManager(this,3)
//        val vmFactory=TopCitiesVMFactory(this.application)
//        viewModel=ViewModelProvider(this,vmFactory).get(SearchViewModel::class.java)
        viewModel=ViewModelProvider(this).get(SearchViewModel::class.java)
        viewModel.topCities.observe(this){
            showTopCities(it)
        }
    }
    fun showTopCities(list:List<String>){
        topCities.clear()
        topCities.addAll(list)
        topCitiesAdapter.notifyDataSetChanged()
    }

}
Caused by: java.lang.RuntimeException: Cannot create an instance of class SearchViewModel
        at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:221)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
        at AddCityActivity.onCreate(AddCityActivity.kt:29)
        at android.app.Activity.performCreate(Activity.java:7255)
        at android.app.Activity.performCreate(Activity.java:7246)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1275)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3099)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3254) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1997) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:224) 
        at android.app.ActivityThread.main(ActivityThread.java:7056) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:536) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:876) 
     Caused by: java.lang.InstantiationException: java.lang.Class<SearchViewModel> has no zero argument constructor
        at java.lang.Class.newInstance(Native Method)
        at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:219)
阅读 3.7k
1 个回答

添加两个依赖:

implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'

implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'

添加第二个依赖后ViewModelProvider(this).get(xxxViewModel.class)创建viewmodel成功

xxxViewModel.class就是继承自AndroidViewModel

https://www.jianshu.com/p/83a...

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏