Could not find property setter method setAlpha
Asked Answered
B

1

6

This is the Java code for creating an alpha animator object.

ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(myView, "alpha", 0.5f, 0f);

This is the Kotlin code for doing the same.

val objectAnimator = ObjectAnimator.ofFloat(myView, "alpha", 0.5f, 0f)

After I converted above Java code to Kotlin, Android Studio is giving me a red error warning with the this message when hover over the line where the error occurs. Could not find property setter method setAlpha on java.lang.Void more

Despite the IDE is giving this error, but I am still able to compile and run it. Any idea why it's giving this error in Kotlin and how to get rid of this error warning?

Bushhammer answered 2/4, 2019 at 16:32 Comment(1)
Try using View.ALPHA instead of alpha as a method parameter. Maybe it helps.Lucindalucine
B
12

This happens because your myView has nullable type, like View?. To get rid of this error convert your view to a non-null type (myView!!):

val objectAnimator = ObjectAnimator.ofFloat(myView!!, "alpha", 0.5f, 0f)
Bringhurst answered 2/4, 2019 at 16:55 Comment(2)
saved my time :)Decembrist
Good guess, but it doesn't work in my case. But the code still compiles with the original error. Probably another AS/kotlin bug--I'm sure they'll fix it in a few years (right when Google decides to switch to yet another "better" language).Rabbitry

© 2022 - 2024 — McMap. All rights reserved.