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视图时出现充气异常_Android_Kotlin_Exception_Imageview_Inflate Exception - Fatal编程技术网

扩展Android视图时出现充气异常

扩展Android视图时出现充气异常,android,kotlin,exception,imageview,inflate-exception,Android,Kotlin,Exception,Imageview,Inflate Exception,我最近在尝试创建扩展AndroidX的AppCompatImageView的自定义类时遇到了一个异常 android.view.InflateException:Binary XML文件行#9 in{package}:layout/all_message_行:Binary XML文件行 //{package}中的#9:layout/all_message_row:膨胀类{package}.ui.view.DotImageView时出错 我在本项目中使用视图绑定,每当您膨胀视图时都会发生这种情况,

我最近在尝试创建扩展AndroidX的AppCompatImageView的自定义类时遇到了一个异常

android.view.InflateException:Binary XML文件行#9 in{package}:layout/all_message_行:Binary XML文件行 //{package}中的#9:layout/all_message_row:膨胀类{package}.ui.view.DotImageView时出错

我在本项目中使用视图绑定,每当您膨胀视图时都会发生这种情况,如:

AllMessageRowBinding.inflate(layoutInflater, parent, false)
下面是班级宣言:

class DotImageView(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0)
: androidx.appcompat.widget.AppCompatImageView(context, attrs, defStyleAttr) {
    // rest of the class logic here
}

我发现您需要像这样添加
@JVMLowloads构造函数

class DotImageView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0)
: androidx.appcompat.widget.AppCompatImageView(context, attrs, defStyleAttr) {
    // rest of the class logic here
}
由于View(以及其他类似ImageView的扩展)有多个构造函数, 通知Kotlin编译器自动生成这些构造函数。否则,在运行时,它在尝试膨胀视图时找不到所需的构造函数之一,并引发异常