Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inflating error #18

Open
FireLord opened this issue Jul 30, 2021 · 8 comments
Open

inflating error #18

FireLord opened this issue Jul 30, 2021 · 8 comments

Comments

@FireLord
Copy link

FireLord commented Jul 30, 2021

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

@ankitchaudhary
Copy link

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)

@vamsireddytalla
Copy link

For me Also Same Error I am getting Sir What is the solution for that

@vamsireddytalla
Copy link

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

@tarunpwar
Copy link

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.

@vamsireddytalla
Copy link

vamsireddytalla commented Sep 23, 2021 via email

@vamsireddytalla
Copy link

vamsireddytalla commented Sep 23, 2021 via email

@TryThisOneMyFriend
Copy link

TryThisOneMyFriend commented Sep 18, 2022

`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.

@cjy2103
Copy link

cjy2103 commented Sep 6, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants
@ankitchaudhary @FireLord @cjy2103 @vamsireddytalla @tarunpwar @TryThisOneMyFriend and others