lateinit 属性适配器尚未初始化设置 RecyclerView 与 Fragment

Posted

技术标签:

【中文标题】lateinit 属性适配器尚未初始化设置 RecyclerView 与 Fragment【英文标题】:lateinit property adapter has not been initialized setting up RecyclerView with Fragment 【发布时间】:2020-06-08 15:09:18 【问题描述】:

我在将 RecyclerView 调整为片段时遇到问题。我有一个导航抽屉,它又具有一个 TabLayout,当我单击一个选项卡时,它会将我带到这个 RecyclerView。没办法适配,现在报“lateinit property adapter has not been initialized”的错误,但我试了上千种方法。

MainAdapter.kt

class MainAdapter(private val context: Context) : RecyclerView.Adapter<MainAdapter.MainViewHolder>() 


private var dataList = mutableListOf<Usuario>()

fun setListData(data:MutableList<Usuario>)
    dataList = data


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainViewHolder 
    val view = LayoutInflater.from(context).inflate(R.layout.item_row, parent, false)
    return MainViewHolder(view)


override fun getItemCount(): Int 
    return if(dataList.size > 0)
        dataList.size
    else
        0
    


override fun onBindViewHolder(holder: MainViewHolder, position: Int) 
    val user = dataList[position]
    holder.bindView(user)


inner class MainViewHolder(itemView: View): RecyclerView.ViewHolder(itemView)

    fun bindView(user: Usuario)
        Glide.with(context).load(user.imageUrl).into(itemView.circleImageView)
        itemView.txt_title.text = user.nombre
        itemView.txt_desc.text = user.descripcion 
    



FragmentNotification.kt

class NotificacionesFragment : Fragment()

private lateinit var adapter: MainAdapter
private lateinit var _view: View
private lateinit var rv: RecyclerView


override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? 
    _view = inflater.inflate(R.layout.notificaciones, container, false)
   // val viewPager = x.findViewById<View>(R.id.viewPager) as ViewPager
    rv = _view.findViewById(R.id.rv) as RecyclerView

    rv.adapter = MainAdapter (requireContext())

    _view.rv.layoutManager = LinearLayoutManager(requireContext())
    _view.rv.adapter = adapter

    val dummyList = mutableListOf<Usuario>()
    dummyList.add(Usuario("lala","lala","lala"))

    adapter.setListData(dummyList)
    adapter.notifyDataSetChanged()

    return _view


【问题讨论】:

【参考方案1】:

错误很明显:

lateinit 属性适配器尚未初始化

您从未设置此属性:

private lateinit var adapter: MainAdapter

您设置了rv.adapter,但绝不仅仅是adapter。相反,您可能打算这样做:

adapter = MainAdapter (requireContext())
rv.adapter = adapter

【讨论】:

你是最棒的!【参考方案2】:
class NotificacionesFragment : Fragment()

private lateinit var adapter: MainAdapter
private lateinit var _view: View
private lateinit var rv: RecyclerView


override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? 
    _view = inflater.inflate(R.layout.notificaciones, container, false)
   // val viewPager = x.findViewById<View>(R.id.viewPager) as ViewPager
    rv = _view.findViewById(R.id.rv) as RecyclerView

    adapter = MainAdapter (requireContext())

    _view.rv.layoutManager = LinearLayoutManager(requireContext())
    _view.rv.adapter = adapter

    val dummyList = mutableListOf<Usuario>()
    dummyList.add(Usuario("lala","lala","lala"))

    adapter.setListData(dummyList)
    adapter.notifyDataSetChanged()

    return _view


【讨论】:

以上是关于lateinit 属性适配器尚未初始化设置 RecyclerView 与 Fragment的主要内容,如果未能解决你的问题,请参考以下文章

@Autowired lateinit 属性 '' 尚未初始化

kotlin.UninitializedPropertyAccessException:lateinit 属性尚未初始化

UninitializedPropertyAccessException:lateinit 属性尚未初始化

UninitializedPropertyAccessException:lateinit 属性首选项尚未初始化

:kotlin.UninitializedPropertyAccessException:lateinit 属性管理器尚未初始化

从 firebase 获取数据:lateinit 属性 currentUser 尚未初始化