【发布时间】: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