Issue
I am using Kotlin in IntelliJ.
I have this short and simple bit of code
val myLayoutView = v.findViewById(R.id.layout) as LinearLayout
var myImageView = v.findViewById(R.id.image) as ImageView
val myLabelView =v.findViewById(R.id.label) as TextView
var app = getItem(position) as AppObject
myLabelView.text = app.appName
myImageView.drawable = app.appImage
Why in the WORLD is it telling me that on the line:
myImageView.drawable = app.appImage
that "Val cannot be reassigned" Any instance of val has been COMPLETELY changed to var with respect to the variables mentioned.
I have already changed variables with the same name in other classes just to be sure and even changed every single variable in the class to var and I STILL get this error.
Does it have to do with the nature of drawable?
What am I missing or doing wrong to get this error and how do I fix it?
Thank you!
Solution
As per the Kotlin Properties and Fields documentation, a val
property is a property with a getter, but no setter. A var
property has both a getter and setter.
If you look at ImageView
, it has a getDrawable()
, but no setDrawable()
method. Kotlin automatically converts the getters in Java classes into properties, which is why you can use myImageView.drawable
at all. However, since there is no setDrawable()
method, that is a val
property.
You'll need to use methods such as setImageDrawable()
to set the image.
Answered By - ianhanniballake
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.