Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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 4.4中的自定义视图构造函数在Kotlin上崩溃,如何修复?_Android_Kotlin_Android Custom View_Android 4.4 Kitkat - Fatal编程技术网

Android 4.4中的自定义视图构造函数在Kotlin上崩溃,如何修复?

Android 4.4中的自定义视图构造函数在Kotlin上崩溃,如何修复?,android,kotlin,android-custom-view,android-4.4-kitkat,Android,Kotlin,Android Custom View,Android 4.4 Kitkat,我有一个用Kotlin编写的自定义视图,使用JVM重载,我可以使用默认值 class MyView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyle: Int = 0, defStyleRes: Int = 0 ) : LinearLayout(context, attrs, defStyle, defStyleRes) 在安卓5.1及以上版本中,

我有一个用Kotlin编写的自定义视图,使用JVM重载,我可以使用默认值

class MyView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0,
    defStyleRes: Int = 0
) : LinearLayout(context, attrs, defStyle, defStyleRes)
在安卓5.1及以上版本中,所有这些都可以正常工作

但是它在4.4中崩溃,因为4.4中的构造函数没有
defstylers
。在5.1及以上版本中,我可以使用
defStyleRes
,但在4.4中却没有,而不需要像在Java中那样显式定义4个构造函数,我怎么能支持它呢

注意:下面的内容在4.4中可以正常工作,但随后我们松开了
defStyleRes

class MyView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0
) : LinearLayout(context, attrs, defStyle)

最好的办法就是这样上课

class MyView : LinearLayout {
    @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : super(context, attrs, defStyleAttr)
    @TargetApi(Build.VERSION_CODES.LOLLIPOP) constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes)
}

我有办法做到这一点。只需重载前3个函数即可,将第4个函数留给棒棒糖,并在上面用@TargetApi包装

class MyView : LinearLayout {
    @JvmOverloads
    constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0)
        : super(context, attrs, defStyleAttr)

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int)
        : super(context, attrs, defStyleAttr, defStyleRes)
}

只需像这样定义构造函数:

constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
@TargetApi(21)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes)

这又回到了定义所有4个构造函数的老方法:(这是Android的工作方式,不管Kotlin的语法有多大帮助,Android需要样板文件:D无论如何,你可以使用默认值来减少这些,但你必须至少使用2constructors@Elye我已经编辑了我的答案,以显示与2个构造函数相同的功能。这与我的问题相同。它将在4.4中崩溃是的,当然,我会添加它。别忘了标记它是正确的答案。这是一个有效的答案。好的@Elye你如何使用这种方法访问init函数中的属性@Elye?@niegus你可以,只需自己调用
初始化
函数你就可以阅读更多关于
@JvmOverloads
在本文中: