Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在子类中定义参数后,如何通过Super发送参数?_Android_Kotlin_Android View_Android Custom View - Fatal编程技术网

Android 在子类中定义参数后,如何通过Super发送参数?

Android 在子类中定义参数后,如何通过Super发送参数?,android,kotlin,android-view,android-custom-view,Android,Kotlin,Android View,Android Custom View,我想通过super将属性发送到视图中。(如第一行所示)但IDE不想接受它;我的代码中缺少一些内容 class BoxDrawingView(context: Context): View(context, attrs) { var attrs: AttributeSet? = null constructor(context: Context, attrs: AttributeSet): this(context) { this.attrs = attrs

我想通过super将属性发送到视图中。(如第一行所示)但IDE不想接受它;我的代码中缺少一些内容

class BoxDrawingView(context: Context): View(context, attrs) {

    var attrs: AttributeSet? = null

    constructor(context: Context, attrs: AttributeSet): this(context) {
        this.attrs = attrs
    }
}
有人能帮我吗?谢谢

试试这个:

class BoxDrawingView : View {

    var attrs: AttributeSet? = null

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        this.attrs = attrs
    }
}
问题是,您没有一个单一的构造函数适用于所有情况。发生这种情况时,您可以避免使用普通的构造函数速记(即,在类声明中内联声明属性),创建多个构造函数,并将每个构造函数委托给不同的超类构造函数

如果希望将
attrs
声明为
val
而不是var,也可以稍微修改一下:

class BoxDrawingView : View {

    val attrs: AttributeSet?

    constructor(context: Context) : super(context) {
        this.attrs = null
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        this.attrs = attrs
    }
}
试试这个:

class BoxDrawingView : View {

    var attrs: AttributeSet? = null

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        this.attrs = attrs
    }
}
问题是,您没有一个单一的构造函数适用于所有情况。发生这种情况时,您可以避免使用普通的构造函数速记(即,在类声明中内联声明属性),创建多个构造函数,并将每个构造函数委托给不同的超类构造函数

如果希望将
attrs
声明为
val
而不是var,也可以稍微修改一下:

class BoxDrawingView : View {

    val attrs: AttributeSet?

    constructor(context: Context) : super(context) {
        this.attrs = null
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        this.attrs = attrs
    }
}

先生,似乎可以。谢谢先生,似乎可以。谢谢