-
-
Notifications
You must be signed in to change notification settings - Fork 761
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
kotlin下init调用setContentView会有构造参数未初始化的问题 #478
Comments
是否可以考虑在BasePopupWindow构造函数中增加一个类似老版本的onCreateContentView回调方法,没有返回值,只是用于告诉子类父构造初始化完成了,子类再覆写这个方法去setContentView |
我记得你的data写到init外部也会当成主构造函数一部分吧? class(xx){ |
搞错了,测试不行 fun main() {
Child("Joe")
// 输出:
// Father constructor Joe
// Child call null
}
open class Father {
constructor(data: String) {
println("Father constructor $data")
childCall()
}
open fun childCall() {}
}
class Child(val data: String) : Father(data) {
override fun childCall() {
println("Child call $data")
}
} |
在constructor中call子类的方法,子类是无法访问的,因为java的加载问题,此时其实仍然在父类的加载过程中,子类的变量还没赋值,是初始值null |
这也是为啥我抛弃了之前lazypopupwindow的原因,只有走到的init时,才开始对子类变量赋值,此时setcontentview才有效 |
提issue前请去WIKI#常见问题查询相关问题,避免重复提问
Please go to WIKI#FAQ for relevant questions before commit your issue to avoid repetitive questions.
提issue前请务必参考以下格式填写,否则该问题会被直接关闭。
Please be sure to follow the format below before submitting the issue, otherwise the question will be actively closed!
kotlin下init调用setContentView会有构造参数未初始化的问题
可以改成constructor()使用,但是这样写起来就比较麻烦,变量要定义在外面,而且idea有黄色提示
还有什么比较好的解决方式吗?
The text was updated successfully, but these errors were encountered: