class HomeFragment : Fragment(), CardStackListener {
val cardStackView = card_stack_view
val manager = CardStackLayoutManager(getActivity(), this)
private val adapter by lazy { CardStackAdapter(createSpots()) }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val homeFragmentView : View = inflater!!.inflate(R.layout.fragment_home, container, false)
cardStackView.layoutManager = manager
setupCardStackView()
setupButton()
return homeFragmentView
}
*错误消息是“ cardStackView不能为空” 错误出现在“ cardStackView.layoutManager = manager”中 我该怎么办才能解决这个问题... *
答案 0 :(得分:0)
在cardStackView
之后添加问号,如下所示:
cardStackView?.layoutManager = manager
我想cardStackView
是可以为空的,IDE表示此行可能会导致NullPointerException
答案 1 :(得分:0)
您可以添加您正在使用哪个CardStackView吗?有几种实现这种视图的库(其中一些具有很好的文档,例如https://github.com/yuyakaido/CardStackView和https://github.com/loopeer/CardStackView)。
您可以查找CardStackView for Android being used on Stack Overflow的示例或使用Google。
更新 该问题可能是由于manager字段可能为空引起的。如果您完全在函数中进行初始化会怎样?
val manager = CardStackLayoutManager()
val adapter = CardStackAdapter()
val cardStackView = findViewById<CardStackView>(R.id.card_stack_view)
cardStackView.layoutManager = manager
cardStackView.adapter = adapter