【发布时间】:2021-09-15 05:52:54
【问题描述】:
我正在做一个项目,我必须使用不同类型的自定义视图。这些自定义视图共享一些通用功能,因此我决定创建一个超类并将所有通用功能放在那里。让我们调用我的超类 BaseCustomView 和子类 ACustomView。
这是我的 BaseCustomView 的样子:
open class BaseCustomView@JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
FrameLayout(context, attrs, defStyleAttr) {
protected fun saveDetails() {
// Saving details
}
}
我从我的子类中调用这个函数。这是我的 ACustomView 的样子:
class ACustomView@JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : BaseCustomView(context, attrs, defStyleAttr) {
fun startProcess() {
saveDetails()
//Start the process
}
}
到这里为止看起来还不错。现在,当我在片段中使用 ACustomView 并使用 viewBinding 调用 startProcess() 函数时,它给了我以下错误:
Unresolved reference: startProcess
这是我在片段中调用函数的代码:
binding.customView.startProcess()
谁能帮我解决这个问题?我不知道我做错了什么。任何帮助表示赞赏。谢谢!
【问题讨论】:
-
请分享您调用
startProcess的代码 -
嗨@ShlomiKatriel,我使用viewBinding 来访问视图并调用该函数。我已经更新了我的问题。
-
我怀疑绑定类在最终的 apk 中生成了
View类而不是ACustomView。我建议在 build 文件夹中找到生成的代码部分并检查绑定类 -
虽然我很高兴它工作了,但我仍然相信有一种方法可以让它与视图绑定一起工作:)。我会写下当前的解决方案作为答案,但如果我找到更好的解决方案,我会更新它
-
删除了
android-studio标签,因为该标签用于解决有关 Android Studio 产品的问题。您的问题是一个通用的 Android 问题。
标签: android android-custom-view