AndroidViewModel 无法创建实例 Kotlin

AndroidViewModel Cannot Create An Instance Kotlin

我已经检查了所有相关主题。但是找不到适合自己的解决方案。我正在使用 ApiCall 加载实时数据,没问题。但现在我需要使用 Roo,这样即使没有互联网连接,用户也可以使用它一段时间。好的,让我向你介绍我的错误。

    2020-11-27 13:11:46.317 15232-15232/com.example.jirataskmvvm E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.jirataskmvvm, PID: 15232
    java.lang.RuntimeException: Cannot create an instance of class com.example.jirataskmvvm.viewModel.CityRmViewModel
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:275)
        at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:106)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:185)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
        at com.example.jirataskmvvm.view.citySelectPage.citySelectionPage.onCreateView(citySelectionPage.kt:29)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:320)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
        at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2629)
        at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2577)
        at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2722)
        at androidx.fragment.app.FragmentStateManager.activityCreated(FragmentStateManager.java:346)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1188)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
        at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2625)
        at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2577)
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:247)
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:541)
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:210)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1435)
        at android.app.Activity.performStart(Activity.java:8024)
        at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3475)
        at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
        at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:267)
        at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:106) 
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:185) 
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150) 
        at com.example.jirataskmvvm.view.citySelectPage.citySelectionPage.onCreateView(citySelectionPage.kt:29) 
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698) 
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:320) 
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187) 
        at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224) 
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997) 
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953) 
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849) 
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2629) 
        at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2577) 
        at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2722) 
        at androidx.fragment.app.FragmentStateManager.activityCreated(FragmentStateManager.java:346) 
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1188) 
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356) 
        at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434) 
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497) 
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2625) 
        at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2577) 
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:247) 
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:541) 
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:210) 
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1435) 
        at android.app.Activity.performStart(Activity.java:8024) 
        at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3475) 
        at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221) 
        at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201) 
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
     Caused by: java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
        at androidx.room.RoomDatabase.assertNotMainThread(RoomDatabase.java:267)
        at androidx.room.RoomDatabase.query(RoomDatabase.java:323)
2020-11-27 13:11:46.317 15232-15232/com.example.jirataskmvvm E/AndroidRuntime:     at androidx.room.util.DBUtil.query(DBUtil.java:83)
        at com.example.jirataskmvvm.Room.CityDao_Impl.readAllCities(CityDao_Impl.java:70)
        at com.example.jirataskmvvm.Room.CityRoomRepository.<init>(CityRoomRepository.kt:17)
        at com.example.jirataskmvvm.viewModel.CityRmViewModel.<init>(CityRmViewModel.kt:28)
            ... 41 more
2020-11-27 13:11:46.335 15232-15232/com.example.jirataskmvvm I/Process: Sending signal. PID: 15232 SIG: 9

如您所见,我收到 RuntimeException,提示我“我无法创建 class 的实例”。我已经搜索过,但我只能找到与普通 ViewModel 相关的东西,而这对我的 AndroidViewModel 不起作用,所有其他 AndroidViewModel 相关主题也没有帮助或不适合我。

这是我的 classes.

class CityRmViewModel (application: Application):AndroidViewModel(application){

    val allCities = MutableLiveData<List<CityRm>>()

    private val cityRoomRepository: CityRoomRepository

    init {
        val cityDao = EventsDatabase.getEventsDatabase(application).cityDao()
        cityRoomRepository = CityRoomRepository(cityDao)
    }

    private fun readFromDB(){
        cityRoomRepository.readAll(allCities)

    }

    fun loadData(){
        readFromDB()
    }

    private fun addCity(cityRm: CityRm){
        viewModelScope.launch(Dispatchers.IO){
            cityRoomRepository.addCity(cityRm)
        }
    }

    fun loadDataToDB(){
        val request = cityService.buildService(CityEndPoint::class.java)
        val call = request.getCities()

        call.enqueue(object : Callback<List<City>> {
            override fun onResponse(call: Call<List<City>>, response: Response<List<City>>) {
                if (response.isSuccessful) {
                    for(i in 0..response.body()!!.size){
                        val cityRm = CityRm(response.body()!![i].id,response.body()!![i].name)
                        addCity(cityRm)
                    }
                }
            }
            override fun onFailure(call: Call<List<City>>, t: Throwable) {
                Log.d("api response:", t.message.toString())
            }
        })
    }
}

我的第一个片段:

class citySelectionPage : Fragment() {

    lateinit var cityViewModel: CityRmViewModel
    private val cityAdapter = cityScPageAdapter(arrayListOf())


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        val view = inflater.inflate(R.layout.fragment_city_selection_page, container, false)

        cityViewModel = ViewModelProvider(this).get(CityRmViewModel::class.java)
        cityViewModel.loadData()


        val recyclerView = view.findViewById<RecyclerView>(R.id.cityPageRecycler)
        recyclerView.apply {
            layoutManager = LinearLayoutManager(activity)
            adapter = cityAdapter
        }

        observeViewModel()
        return view
    }

    fun observeViewModel(){
        cityViewModel.allCities.observe(viewLifecycleOwner, { cities -> cities?.let {
                cityAdapter.updateCities(it)
        }
        })
    }
}

最后,我不想在视图中进行逻辑操作,至少我可以设法避免多少,因为我刚开始作为一名新研究生的新工作。这是我的培训,我需要为公司项目编写清晰的代码

堆栈跟踪的相关部分是这个

Caused by: java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.

您需要在非 UI 线程上查询您的数据库。对此有很多选择,我建议搜索有关 Kotlin Coroutines 的信息,因为这是目前有据可查且受支持的功能。