Open FireLord opened 3 years ago
Caused by: android.view.InflateException: Binary XML file line #24: Binary XML file line #24: Error inflating class fragment Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class fragment Caused by: kotlin.UninitializedPropertyAccessException: lateinit property viewModel has not been initialized at com.android.newapp.ui.NewsActivity.getViewModel(NewsActivity.kt:14) at com.android.newapp.ui.fragments.BreakingNewsFragment.onViewCreated(BreakingNewsFragment.kt:24)
For me Also Same Error I am getting Sir What is the solution for that
Caused by: android.view.InflateException: Binary XML file line #25 in com.talla.binchecker:layout/activity_main: Binary XML file line #25 in com.talla.binchecker:layout/activity_main: Error inflating class fragment Caused by: android.view.InflateException: Binary XML file line #25 in com.talla.binchecker:layout/activity_main: Error inflating class fragment Caused by: kotlin.UninitializedPropertyAccessException: lateinit property binViewModel has not been initialized
I replaced this line "viewModel = (activity as NewsActivity).viewModel" from every fragment with this
"val newsRepository = NewsRepository(ArticleDatabase(requireContext())) val vmProviderFactory = NewsVMProviderFactory(newsRepository) viewModel = ViewModelProvider(this, vmProviderFactory).get(NewsViewModel::class.java) " and it worked.
Same thing worked to me bro
On Thu, 23 Sep, 2021, 11:13 am Tarun Kumar, @.***> wrote:
I replaced this line "viewModel = (activity as NewsActivity).viewModel" from every fragment with this
"val newsRepository = NewsRepository(ArticleDatabase(requireContext())) val vmProviderFactory = NewsVMProviderFactory(newsRepository) viewModel = ViewModelProvider(this, vmProviderFactory).get(NewsViewModel::class.java) " and it worked.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/philipplackner/MVVMNewsApp/issues/18#issuecomment-925520704, or unsubscribe https://github.com/notifications/unsubscribe-auth/AML7WRDHNOYNF6EL4Z2KPALUDK5ARANCNFSM5BICFNFQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.
But I have doubt why this line not working
viewModel = (activity as NewsActivity).viewModel"
`Hi guys, the solution is quite simple...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val newsRepository = NewsRepository(ArticleDatabase(this))
val viewModelProviderFactory = NewsViewModelProviderFactory(newsRepository)
viewModel = ViewModelProvider(this, viewModelProviderFactory).get(NewsViewModel::class.java)
setContentView(R.layout.activity_news)
bottomNavigationView.setupWithNavController(newsNavHostFragment.findNavController())
}
`
The view must be created after the ViewModel setup, since every fragment will be refering the NewsActivity.
That will do the trick for sure.
Don't use FrameLayout FrameLayout's lifecycle management is poor and causes strange behavior. In the above case, the problem occurs because BreakingNewsFragment onViewCreated life cycle occurs before News Activity onCreate.
So change it like this
activity_news.xml
<androidx.fragment.app.FragmentContainerView
android:id="@+id/newsNavHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/news_nav_graph" />
NewsActivity
class NewsActivity : AppCompatActivity() {
lateinit var viewModel: NewsViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_news)
val newsRepository = NewsRepository(ArticleDatabase(this))
val viewModelProviderFactory = NewsViewModelProviderFactory(newsRepository)
viewModel = ViewModelProvider(this, viewModelProviderFactory)[NewsViewModel::class.java]
val navHostFragment = supportFragmentManager.findFragmentById(R.id.newsNavHostFragment)
val navController: NavController = navHostFragment!!.findNavController()
NavigationUI.setupWithNavController(bottomNavigationView, navController)
}
}
androidx.fragment.app.FragmentContainerView is a part of the AndroidX library that improved the shortcomings of FrameLayout.
im getting
Caused by: android.view.InflateException: Binary XML file line #25 in com.androiddevs.mvvmnewsapp:layout/activity_news: Binary XML file line #25 in com.androiddevs.mvvmnewsapp:layout/activity_news: Error inflating class fragment Caused by: android.view.InflateException: Binary XML file line #25 in com.androiddevs.mvvmnewsapp:layout/activity_news: Error inflating class fragment Caused by: kotlin.UninitializedPropertyAccessException: lateinit property viewModel has not been initialized at com.androiddevs.mvvmnewsapp.ui.NewsActivity.getViewModel(NewsActivity.kt:15) at com.androiddevs.mvvmnewsapp.ui.fragments.BreakingNewsFragment.onViewCreated(BreakingNewsFragment.kt:25)
and app crashes