我知道这些问题已经得到解答,但我认为这是另一回事。所以,我有以下代码:
viewPager = findViewById<View>(R.id.viewPagerGaleria) as ViewPager
val adapter = ViewPageAdapter(this)
viewPager.adapter = adapter
//dots
val pageIndicatorView = findViewById<View>(R.id.pageIndicatorView) as PageIndicatorView
pageIndicatorView.setViewPager(viewPager)
当我运行它时,会发生这种情况: ... kotlin.TypeCastException: null 不能转换为非 null 类型 android.support.v4.view.ViewPager。正常到这里,对吧?不,因为当我这样做时,错误是一样的:
val viewPager = findViewById<View>(R.id.viewPagerGaleria) as? ViewPager
val adapter = ViewPageAdapter(this)
viewPager?.adapter = adapter
//dots
val pageIndicatorView = findViewById<View>(R.id.pageIndicatorView) as? PageIndicatorView
pageIndicatorView?.setViewPager(viewPager)
你能帮助我吗?谢谢你。