Android fragments kotlin片段错误NullPointerException

Android fragments kotlin片段错误NullPointerException,android-fragments,kotlin,android-context,Android Fragments,Kotlin,Android Context,我的完整代码 class BlankFragment : Fragment() { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val rootView = inflater.inflate(R.layout.mainex, container, false) val gr

我的完整代码

class BlankFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val rootView = inflater.inflate(R.layout.mainex, container, false)

        val groupAdapter = GroupAdapter<ViewHolder>().apply {
            spanCount = 2
        }

        recycler_view.apply {
            //error NullPointerException in this line
            layoutManager = GridLayoutManager(rootView.context, groupAdapter.spanCount).apply {
                spanSizeLookup = groupAdapter.spanSizeLookup
            }
            adapter = groupAdapter
        }


        var headerTab: ArrayList<mTop>
        headerTab = arguments?.getSerializable("headertab") as ArrayList<mTop>


        for (h in 0 until headerTab.size) {
            val header = headerTab.get(h).kategori

            ExpandableGroup(ExpandableHeaderItem(header), true).apply {

                for (c in 0 until headerTab[h].sub.size) {
                    val gambar = (headerTab[h].sub).get(c).gambar
                    val nama_menu = (headerTab[h].sub).get(c).nama_menu
                    add(Section(FancyItem(gambar, nama_menu)))
                }

                groupAdapter.add(this)
            }

        }


谢谢:)(对不起,我的英语不好)

在创建视图之前,您正在将LayoutManager设置为recyclerView。您应该在onViewCreated()方法或xml文件中执行此操作。使用onCreateView()仅用于膨胀视图。然后将onViewCreated()用于您需要对视图执行的其他设置。

完成。这是我的完整修复代码,包含单独的onCreateView和onViewCreate。谢谢大家

class FragBaru : Fragment() {

    private lateinit var rv: RecyclerView

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.mainex, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

        rv = view.findViewById(R.id.recycler_view)

        val groupAdapter = GroupAdapter<ViewHolder>().apply {
            spanCount = 2
        }

        rv.apply {
            layoutManager = GridLayoutManager(rootView.context, groupAdapter.spanCount).apply {
                spanSizeLookup = groupAdapter.spanSizeLookup
            }
            adapter = groupAdapter
        }

        var headerTab: ArrayList<mTop>
        headerTab = arguments?.getSerializable("headertab") as ArrayList<mTop>


        for (h in 0 until headerTab.size) {
            val header = headerTab.get(h).kategori

            ExpandableGroup(ExpandableHeaderItem(header), true).apply {

                for (c in 0 until headerTab[h].sub.size) {
                    val gambar = (headerTab[h].sub).get(c).gambar
                    val nama_menu = (headerTab[h].sub).get(c).nama_menu
                    add(Section(FancyItem(gambar, nama_menu)))
                }

                groupAdapter.add(this)
            }

        }
    }


    companion object {
        fun newInstance(headertab: ArrayList<mTop>): FragBaru {
            val f = FragBaru()

            val args = Bundle()
            args.putSerializable("headertab", headertab)

            f.setArguments(args)
            return f
        }
    }


}
class FragBaru:Fragment(){
私有lateinit变量rv:RecyclerView
覆盖创建视图(充气机:布局充气机,容器:ViewGroup?,savedInstanceState:Bundle?):视图{
返回充气机。充气(R.layout.mainex,容器,假)
}
覆盖已创建的视图(视图:视图,保存状态:捆绑?){
rv=视图。findViewById(R.id.recycler\U视图)
val groupAdapter=groupAdapter()。应用{
spanCount=2
}
rv.apply{
layoutManager=GridLayoutManager(rootView.context,groupAdapter.spanCount)。应用{
spanSizeLookup=groupAdapter.spanSizeLookup
}
适配器=组适配器
}
var headerTab:ArrayList
headerTab=参数?.getSerializable(“headerTab”)作为ArrayList
用于(0中的h,直到表头选项卡大小){
val header=headerTab.get(h).kategori
ExpandableGroup(ExpandableHeaderItem(标头),true)。应用{
对于(0中的c,直到headerTab[h]。子大小){
val gambar=(headerTab[h].sub.get(c).gambar
val nama_menu=(headerTab[h].sub.get(c).nama_menu
添加(部分(FancyItem(甘巴、纳马菜单)))
}
groupAdapter.add(这个)
}
}
}
伴星{
有趣的新实例(headertab:ArrayList):FragBaru{
val f=FragBaru()
val args=Bundle()
args.putSerializable(“headertab”,headertab)
f、 设置参数(args)
返回f
}
}
}

您能提供stacktrace吗?当然可以。下面是谁初始化回收器视图?您的rootView.context是正常的。回收器视图为空。看起来你用的是kotlin合成的。检查是否绑定了正确的xml文件是,完成。我使用onCreateView和onViewCreated。非常感谢你,你说得对。这是通过使用onCreateView和onViewCreate完成的。而在其他情况下,我只使用onCreateView就成功了。好的,非常感谢你
class FragBaru : Fragment() {

    private lateinit var rv: RecyclerView

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.mainex, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

        rv = view.findViewById(R.id.recycler_view)

        val groupAdapter = GroupAdapter<ViewHolder>().apply {
            spanCount = 2
        }

        rv.apply {
            layoutManager = GridLayoutManager(rootView.context, groupAdapter.spanCount).apply {
                spanSizeLookup = groupAdapter.spanSizeLookup
            }
            adapter = groupAdapter
        }

        var headerTab: ArrayList<mTop>
        headerTab = arguments?.getSerializable("headertab") as ArrayList<mTop>


        for (h in 0 until headerTab.size) {
            val header = headerTab.get(h).kategori

            ExpandableGroup(ExpandableHeaderItem(header), true).apply {

                for (c in 0 until headerTab[h].sub.size) {
                    val gambar = (headerTab[h].sub).get(c).gambar
                    val nama_menu = (headerTab[h].sub).get(c).nama_menu
                    add(Section(FancyItem(gambar, nama_menu)))
                }

                groupAdapter.add(this)
            }

        }
    }


    companion object {
        fun newInstance(headertab: ArrayList<mTop>): FragBaru {
            val f = FragBaru()

            val args = Bundle()
            args.putSerializable("headertab", headertab)

            f.setArguments(args)
            return f
        }
    }


}