gpt4 book ai didi

android - Hilt 不会注入(inject) Scoped ViewModel

转载 作者:行者123 更新时间:2023-12-05 00:18:47 33 4
gpt4 key购买 nike

我正在尝试将依赖项注入(inject)到导航图范围的 ViewModel
这是我的应用程序:

@HiltAndroidApp
class App : Application()
托管我的 fragment 的 Activity :
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

findNavController(R.id.nav_host_fragment).navigate(R.id.main_graph)
}
}
我的导航图:
<?xml version="1.0" encoding="utf-8"?>
<navigation
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_graph"
app:startDestination="@id/mainFragment">

<fragment
android:id="@+id/mainFragment"
android:name="com.example.hilt.MainFragment" />

</navigation>
我要注入(inject) ViewModel 的对象:
class Repo @Inject constructor()
我的 View 模型:
@HiltViewModel
class MainViewModel @Inject constructor(
val repo: Repo,
application: Application
) : AndroidViewModel(application) {
fun test(){}
}
我注入(inject) View 模型的 fragment :
@AndroidEntryPoint
class MainFragment : Fragment() {

val viewModel: MainViewModel by viewModels()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

viewModel.test()
}
}
现在在这一点上一切都正常工作,但只要我尝试使用这个:
@AndroidEntryPoint
class MainFragment : Fragment() {

val viewModel: MainViewModel by navGraphViewModels(R.id.main_graph)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

viewModel.test()
}
}
该应用程序无法创建我的 ViewModel 并崩溃并留下此堆栈跟踪:
 Caused by: java.lang.RuntimeException: Cannot create an instance of class com.example.hilt.MainViewModel
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:269)
at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:112)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:185)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:54)
at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:41)
at com.example.hilt.MainFragment.getViewModel(Unknown Source:2)
at com.example.hilt.MainFragment.onCreate(MainFragment.kt:17)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2936)
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:472)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:278)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2177)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2094)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1990)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3122)
at androidx.fragment.app.FragmentManager.dispatchCreate(FragmentManager.java:3045)
at androidx.fragment.app.Fragment.onCreate(Fragment.java:1867)
at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java:264)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2936)
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:472)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:278)
at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:141)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:313)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:292)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
显然 by navGraphViewModels速记没有使用正确的 VM Factory,我在这里有点不知所措。

最佳答案

使用hiltNavGraphViewModels来自 androidx.hilt:hilt-navigation-fragment而不是 navGraphViewModels https://developer.android.com/training/dependency-injection/hilt-jetpack#viewmodel-navigation

关于android - Hilt 不会注入(inject) Scoped ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66497047/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com