'Error inflating class layout with fragment

I'm trying to show BreakingNewsFragment through news activity using navigation but the error is show

I am getting this error when trying to run this app. I have followed numerous guides to no avail. I am just trying to do the simplist exercise with navigation

Caused by: android.view.InflateException: Binary XML file line #20 in com.example.newsapp:layout/activity_news: Binary XML file line #20 in com.example.newsapp:layout/activity_news: Error inflating class fragment
 Caused by: android.view.InflateException: Binary XML file line #20 in com.example.newsapp:layout/activity_news: Error inflating class fragment
 Caused by: kotlin.UninitializedPropertyAccessException: lateinit property newsViewModel has not been initialized
    at com.example.newsapp.ui.activity.NewsActivity.getNewsViewModel(NewsActivity.kt:17)
    at com.example.newsapp.ui.fragments.BreakingNewsFragment.onViewCreated(BreakingNewsFragment.kt:29)

newsActivity:

class NewsActivity : AppCompatActivity() {

lateinit var newsViewModel: NewsViewModel

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

    var newsRepository = NewsRepository(ArticleDataBase(this))
    var newsViewModelFactoryProvider = NewsViewModelFactory(newsRepository)
    newsViewModel = ViewModelProvider(this,newsViewModelFactoryProvider).get(NewsViewModel::class.java)

    bottomNavigationView.setupWithNavController(navHostFragmentNews.findNavController())


}}

when to get newsViewModel from activity the error is show

in fragment:

class BreakingNewsFragment : Fragment(R.layout.fragment_breaking_news) {

lateinit var newsViewModel : NewsViewModel
lateinit var articleRecyclerAdapter: ArticleRecyclerAdapter

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    newsViewModel = (activity as NewsActivity).newsViewModel
    setupRecyclerView()
    }

fun setupRecyclerView(){
    articleRecyclerAdapter= ArticleRecyclerAdapter()
    rvBreakingNews.adapter= articleRecyclerAdapter
}}

xml code:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.activity.NewsActivity">


<FrameLayout
    android:id="@+id/flFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <fragment
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:id="@+id/navHostFragmentNews"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph_news"/>

</FrameLayout>

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottomNavigationView"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    app:menu="@menu/menu_bottom_navigation_news"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"/>

 </androidx.constraintlayout.widget.ConstraintLayout>

i use many fragment other it ,with them no any problem why the error come in this fragment



Solution 1:[1]

you are initializing the newsViewModel after doing setContentView. So the fragment gets created before that. Try moving setContentView down, like this:

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

    var newsRepository = NewsRepository(ArticleDataBase(this))
    var newsViewModelFactoryProvider = NewsViewModelFactory(newsRepository)
    newsViewModel = ViewModelProvider(this,newsViewModelFactoryProvider).get(NewsViewModel::class.java)

    setContentView(R.layout.activity_news)

    bottomNavigationView.setupWithNavController(navHostFragmentNews.findNavController())


}}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ivo Beckers