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.layoutManager = manager"中我能做些什么来解决这个问题... *
cardStackView
后添加一个问号,如下所示:
cardStackView?.layoutManager = manager
我想cardStackView
是可为空的,IDE 说这条线可能会导致NullPointerException
你能添加你正在使用的CardStackView吗?有几个库实现了这样的视图(其中一些有很好的文档,如 https://github.com/yuyakaido/CardStackView 和 https://github.com/loopeer/CardStackView)。
你可以寻找在Stack Overflow上使用的CardStackView for Android的例子,或者使用Google。
更新此问题可能是由管理器字段可能为空引起的。如果完全在函数内进行初始化会发生什么?
val manager = CardStackLayoutManager()
val adapter = CardStackAdapter()
val cardStackView = findViewById<CardStackView>(R.id.card_stack_view)
cardStackView.layoutManager = manager
cardStackView.adapter = adapter