Could not find property setter method setAlpha

s-hunter :

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?

Andrew Churilo :

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)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=117390&siteId=1