gpt4 book ai didi

android - Dagger-Hilt @ViewModelInject sharedViewModel 不注入(inject) fragment

转载 作者:行者123 更新时间:2023-12-03 18:28:31 27 4
gpt4 key购买 nike

我正在尝试使用 Jetpack tutorial 在 fragment 和 Activity 之间创建共享注入(inject) View 模型.
共享 View 模型成功注入(inject)到父级 MyActivity 中,但是当渲染子级时,由于依赖注入(inject)失败,应用程序崩溃。我在下面提供了创建问题的代码。
提供 session 管理器:

@InstallIn(ApplicationComponent::class)
@Module
class AppModule {
@Provides
@Singleton
fun provideSessionManager(
networkClient: NetworkClient
): SessionManager {
return SessionManager(networkClient)
}
}
注入(inject)共享 View 模型:
class SharedViewModel @ViewModelInject constructor(
private var sessionManager: SessionManager
) : ViewModel() {

var name = MutableLiveData<String>("Shared View Model")
}
并且被父 Activity 和子 fragment 使用。
class MyActionFragment() : Fragment() {
private val viewModel: SharedViewModel by viewModels()
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
Timber.d("View Model Name 1: ${viewModel.name.value}") // This line crashes
}
}
class MyActivity : AuthenticatedBaseActivity() {
private val viewModel: SharedViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Timber.d("View Model Name 2: ${viewModel.name.value}") // This line prints
}
}
但是,当代码运行时,请注意 Activity 创建了 ViewModel 并访问了它的值,但是当 fragment 尝试这样做时,应用程序崩溃了:
**D/MyActivity: View Model Name 2: Shared View Model**
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.xxx.xxx, PID: 16630
java.lang.RuntimeException: Cannot create an instance of class com.xxx.xxx.ui.main.SharedViewModel
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:221)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:278)
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 androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:54)
at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:41)
at com.xxx.xxx.ui.main.MyActionFragment.getViewModel(Unknown Source:2)
at com.xxx.xxx.ui.main.MyActionFragment.onActivityCreated(**MyActionFragment.kt:140**)
at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2718)
此外,当我删除注入(inject)的 Hilt 依赖项 sessionManager fragment 和 View 模型的创建没有问题。
关注 this post没有运气。
非常感谢有关使用共享模型的 Hilt View 模型依赖注入(inject)的任何帮助!谢谢!

最佳答案

您可以在 中使用扩展功能 fragment :

class MyFragment: Fragment() {
private val viewModel: SharedViewModel by activityViewModels()
}
Activity :
class MyActivity : Activity() {
private val viewModel: SharedViewModel by viewModels()
}

关于android - Dagger-Hilt @ViewModelInject sharedViewModel 不注入(inject) fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63077578/

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