Why does
val fabOpen = view.findViewById(R.id.fab_open) as FloatingActionButton
work correctly and not error out but the kotlin synthetic of
val fabOpen = R.id.fab_open as FloatingActionButton
gives me the
java.lang.Integer cannot be cast to android.support.design.widget.FloatingActionButton
error? They both show that they are casting as FloatingActionButton
. Using the synthetics is not only less code, it's better memory management and I'd prefer to do it this way. Is there something I am missing?
****Update**** I forgot to mention I am trying access the FloatingActionButton
inside of a fragment if that makes a difference.
R.id.fab_open
is a generated integer value that will be set as the ID of your button at inflation, and that you can look up with findViewById
like you've shown.
Casting this to a button won't work, think (FloatingActionButton) 2688664731
in Java terms.
If you wish to use Kotlin Android Extensions and its synthetic properties, those are simply named as the ID itself, but they don't come from the R
class - and you don't need to assign them to variables or properties. You can simply use your button like this:
fab_open.setOnClickListener { ... }
fab_open.visibility = View.VISIBLE