he is coding

Kotlin踩坑(持续更新ing)

| Comments

记录kotlin使用过程中的一些小坑,注意点

  • fragment中使用ktx插件,需要在onViewCreated中使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        val rootView = inflater.inflate(R.layout.fragment_home, container, false)
        //不可以在这里使用recycler_view,会报npe
        return rootView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val homeButtonAdapter = HomeButtonAdapter()
        recycler_view.adapter = homeButtonAdapter
        recycler_view.layoutManager = GridLayoutManager(context, 3)

    }

Comments