java找不到属性设置程序方法setAlpha
这是用于创建alpha animator对象的Java代码
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(myView, "alpha", 0.5f, 0f);
这是执行相同操作的Kotlin代码
val objectAnimator = ObjectAnimator.ofFloat(myView, "alpha", 0.5f, 0f)
在我将上面的Java代码转换为Kotlin之后,当鼠标悬停在发生错误的行上时,Android Studio会给我一个红色错误警告,并显示此消息。 在java上找不到属性设置程序方法setAlpha。lang.Void more
尽管IDE给出了这个错误,但我仍然能够编译并运行它。你知道它为什么会在Kotlin中出现这个错误,以及如何消除这个错误警告吗
# 1 楼答案
发生这种情况是因为
myView
具有可为空的类型,如View?
。 要消除此错误,请将视图转换为非空类型(myView!!
):