小编Out*_*ash的帖子

Kotlin父子类初始化顺序

我有一个视图SubEpoxyRecyclerView,它是EpoxyRecyclerView我的父类的子类。当初始化此视图时,超类调用构造函数主体中的EpoxyRecyclerView方法。setItemSpacingPx(Int)

当调用这个方法时,我的类变量都没有被初始化!应用程序在线崩溃,itemDecorator.pxBetweenItems指出该 itemDecorator值为空,这是不可能的

子类(科特林):

class SubEpoxyRecyclerView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0)
    : EpoxyRecyclerView(context, attrs, defStyleAttr) {

    private val itemDecorator: Decor = Decor()
    private val someInt: Int = 5
    private var someBoolean: Boolean = true

    override fun setItemSpacingPx(spacingPx: Int) {
        // Called from superclass. Debug: itemDecorator is null, 
        // someInt is 0, someBoolean is false

        removeItemDecoration(itemDecorator)
        itemDecorator.pxBetweenItems = spacingPx

        if (spacingPx > 0) {
            addItemDecoration(itemDecorator) …
Run Code Online (Sandbox Code Playgroud)

java android android-view kotlin

0
推荐指数
1
解决办法
2037
查看次数

标签 统计

android ×1

android-view ×1

java ×1

kotlin ×1