Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not find property setter method setAlpha

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?

like image 409
s-hunter Avatar asked Apr 02 '19 16:04

s-hunter


1 Answers

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)
like image 100
Andrew Churilo Avatar answered Nov 16 '22 23:11

Andrew Churilo