0

我正在编写用于在一个片段中获取联系人的代码,而另一个片段具有联系人和电子邮件 ID,但在联系人片段中,存在运行时错误,我无法纠正它是学生和学习者,所以请检查一下,

 recyclerView = view!!.findViewById<RecyclerView>(R.id.demo_recyclerview).apply{

            setHasFixedSize(true)


            layoutManager = viewManager


            adapter =demoadapter

        }

在此处输入图像描述

4

1 回答 1

0

onCreateView在首先膨胀之前,您正在编写代码。这就是为什么访问view会出错。将您的代码移动到onViewCreated方法并且不要使用!!,如果对象为空,它会引发错误。改为使用?

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    recyclerView = view.findViewById<RecyclerView>(R.id.demo_recyclerview).apply {
        setHasFixedSize(true)
        layoutManager = viewManager
        adapter = demoadapter
    }
}
于 2019-08-14T05:46:32.897 回答