Giter VIP home page Giter VIP logo

Comments (8)

tarunpwar avatar tarunpwar commented on August 29, 2024 3

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.

from mvvmnewsapp.

ankitchaudhary avatar ankitchaudhary commented on August 29, 2024

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)

from mvvmnewsapp.

vamsireddytalla avatar vamsireddytalla commented on August 29, 2024

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

from mvvmnewsapp.

vamsireddytalla avatar vamsireddytalla commented on August 29, 2024

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

from mvvmnewsapp.

vamsireddytalla avatar vamsireddytalla commented on August 29, 2024

from mvvmnewsapp.

vamsireddytalla avatar vamsireddytalla commented on August 29, 2024

from mvvmnewsapp.

TryThisOneMyFriend avatar TryThisOneMyFriend commented on August 29, 2024

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

from mvvmnewsapp.

cjy2103 avatar cjy2103 commented on August 29, 2024

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.

from mvvmnewsapp.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.