当前位置: 首页 > 知识库问答 >
问题:

智能转换为“RecyclerView!”是不可能的,因为“recentRecycler”是一个可变属性,可能已经在此时被更改[duplicate]

米景辉
2023-03-14

我尝试将我的代码从java转换为kotlin,因此我面临这个错误。我不明白为什么我会得到这个错误

错误

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

我只是附加给我一个错误的代码并突出显示它

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个答案

隗轶
2023-03-14

在从适配器向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)
   
}
 类似资料: