0

I tried converting my code from java to kotlin and hence I am facing this error. I don't understand why am I getting this error

Error

Smart cast to 'RecyclerView!' is impossible, because 'recentRecycler' is a mutable property that could have been changed by this time
Smart cast to 'RecyclerView!' is impossible, because 'recentRecycler' is a mutable property that could have been changed by this time
Smart cast to 'RecyclerView!' is impossible, because 'topPlacesRecycler' is a mutable property that could have been changed by this time
Smart cast to 'RecyclerView!' is impossible, because 'topPlacesRecycler' is a mutable property that could have been changed by this time

I am only attaching the code which is giving me an error and highlightin it too

private fun setRecentRecycler(recentsDataList: List<RecentsData>) {
        recentRecycler = findViewById(R.id.recent_recycler)
        val layoutManager: RecyclerView.LayoutManager =
            LinearLayoutManager(this, RecyclerView.HORIZONTAL, false)
        **recentRecycler.setLayoutManager(layoutManager)** //error
        recentsAdapter = RecentsAdapter(this, recentsDataList)
        **recentRecycler.setAdapter(recentsAdapter)**//error
    }

    private fun setTopPlacesRecycler(topPlacesDataList: List<TopPlacesData>) {
        topPlacesRecycler = findViewById(R.id.top_places_recycler)
        val layoutManager: RecyclerView.LayoutManager =
            LinearLayoutManager(this, RecyclerView.VERTICAL, false)
        **topPlacesRecycler.setLayoutManager(layoutManager)**//error
        topPlacesAdapter = TopPlacesAdapter(this, topPlacesDataList)
        **topPlacesRecycler.setAdapter(topPlacesAdapter)**//error
    } 

1 Answers1

0

The screen is trying to initialize before data is received from the Adapter to the Layoutmanager.

    private fun setRecentRecycler(recentsDataList: List<RecentsData>) {
    recentRecycler = findViewById(R.id.recent_recycler)
    topPlacesAdapter = TopPlacesAdapter(this, recentsDataList)
    val layoutManager: RecyclerView.LayoutManager =
        LinearLayoutManager(this@Fragment/Activity, RecyclerView.HORIZONTAL, false)
   
}


    private fun setTopPlacesRecycler(topPlacesDataList: List<TopPlacesData>) {
    topPlacesRecycler = findViewById(R.id.top_places_recycler)
    recentsAdapter = RecentsAdapter(this, topPlacesDataList)
    val layoutManager: RecyclerView.LayoutManager =
    LinearLayoutManager(this@Fragment/Activity, RecyclerView.VERTICAL, false)
   
}
Gece
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 23 '22 at 11:01