【问题标题】:kotlin fragment error NullPointerExceptionkotlin 片段错误 NullPointerException
【发布时间】:2019-11-20 10:38:54
【问题描述】:

我的完整代码

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

        }

我正在尝试让 recyclerview 显示在 tablayout 片段中 并发生错误,可能问题来自rootView.context

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

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

【问题讨论】:

  • 你能提供stacktrace吗?
  • 是的,当然。下面
  • 你如何初始化recycler_view?
  • 你的rootView.context是正常的。 recycler_view 为空。看起来您使用的是 kotlin 合成的。检查是否绑定了正确的 xml 文件
  • 是的,完成。我使用 onCreateView 和 onViewCreated。非常感谢

标签: android-fragments kotlin android-context


【解决方案1】:

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

【讨论】:

  • 是的,你是对的。这通过使用 onCreateView 和 onViewCreate 完成。而在其他情况下,我只使用 onCreateView 就成功了。好的,非常感谢你
【解决方案2】:

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


}

【讨论】:

    猜你喜欢
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    • 2014-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-07
    相关资源
    最近更新 更多