kotlin片段错误NullPointerException

问题描述 投票:0回答:2

我的完整代码

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)
            }

        }

我正在尝试在tablayout片段中显示recyclerview并且发生了错误,可能是由于rootView.context

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

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

android-fragments kotlin android-context
2个回答
0
投票

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


0
投票

完成。这是我完整的修复代码,带有单独的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
        }
    }


}
© www.soinside.com 2019 - 2024. All rights reserved.