提问人:Raşit Melih Dinçer 提问时间:10/18/2023 更新时间:10/18/2023 访问量:28
MainActivity 中的 ViewModelProvider [duplicate]
ViewModelProvider in MainActivity [duplicate]
问:
我尝试了一些东西,但ı无法修复此错误,这是我的mainactivitycode
class MainActivity : AppCompatActivity() {
private lateinit var binding : ActivityMainBinding
private lateinit var navController: NavController
lateinit var viewModel : NewsViewModel
val db : ArticleDatabase? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val newsRepository = NewsRepository(ArticleDatabase(this))
val viewModelProviderFactory = NewsViewModelProviderFactory(newsRepository)
viewModel = ViewModelProvider(this,viewModelProviderFactory)[NewsViewModel::class.java]
navController = Navigation.findNavController(this, R.id.newsNavHostFragment)
setupWithNavController(binding.bottomNavigationView,navController)
}
}
这是片段代码
class BrakingNewsFragment : Fragment() {
lateinit var viewModel : NewsViewModel
lateinit var newsAdapter : NewsAdapter
private lateinit var binding : FragmentBrakingNewsBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
binding = FragmentBrakingNewsBinding.inflate(inflater,container,false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel = (activity as MainActivity).viewModel
setUpRecyclerView()
viewModel.breakingNews.observe(viewLifecycleOwner, Observer { response ->
when(response){
is Resource.Succes -> {
response.data?.let {
newsAdapter.differ.submitList(it.articles)
}
}
is Resource.Loading -> {
println("abcabc")
}
}
})
}
private fun setUpRecyclerView(){
newsAdapter = NewsAdapter()
binding.breakingNewsRecyclerView.apply {
adapter = newsAdapter
layoutManager = LinearLayoutManager(activity)
}
}
}
ı 使用的 View ModelProvider 这是代码:
class NewsViewModelProviderFactory(private val newsRepository: NewsRepository) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return NewsViewModel(newsRepository) as T
}
}
logcat 错误在这里
原因:android.view.InflateException:com.example.newsappkotlin:layout/activity_main中的二进制 XML 文件行 #19:com.example.newsappkotlin:layout/activity_main 中的二进制 XML 文件行 #19:扩充类片段时出错 原因:android.view.InflateException:com.example.newsappkotlin:layout/activity_main中的二进制 XML 文件行 #19:膨胀类片段时出错 原因:kotlin。UninitializedPropertyAccessException:lateinit 属性 viewModel 尚未初始化
如何解决 ı
答:
1赞
Kristy Welsh
10/18/2023
#1
您不能以这种方式实例化视图模型:
viewModel = (activity as MainActivity).viewModel
为什么不使用
val viewModel : NewsViewModel by viewModels()
或者,因为这可能是一个带有活动的 sharedViewModel:
val viewModel: NewsViewModel by activityViewModels()
在执行此操作之前,您需要 viewModel 的 kotlin 扩展依赖项。
dependencies {
implementation "androidx.fragment:fragment-ktx:1.6.1"
}
评论
0赞
Raşit Melih Dinçer
10/18/2023
ı 尝试过但未修复 logcat:膨胀类片段时出错
评论
<fragment>
activity_main.xml
FragmentContainerView
<fragment>
onCreateView
setContentView
viewModel