Overload Resolution Ambiguity error in kotlin
You are defining ivFoodImage
in both of your layouts. And you are importing their definitions like so...
import kotlinx.android.synthetic.main.activity_food_details.view.*
import kotlinx.android.synthetic.main.food_ticket.view.*
Consider changing the name in one of the layouts, or being explicit in the definition of foodView
, or removing the import with activity_food_details
if it's not being used.
EDIT
To clarify the possible solutions...
- "Changing the name" - in one of your layouts, change ivFoodImage to something else like
ivFoodImage_Details
. - "removing the unused import" - self explanatory and a good practice.
- "being explicit" - remove the import for the one you want to be explicit with and then do what the OP is doing i.e.,
var foodView = inflator.inflate(R.layout.food_ticket,null)
, explicitly loading fromfood_ticket
in this case.
The concept of using the same name in multiple layouts is not bad (think in terms of interfaces and injection). But the kotlinx.android.synthetic
is a syntactic candy to make things less verbose. It gets in the way of the goal here.
Here is yet another alternative. If you are trying to have a layout implement a sort of "Interface", consider wrapping each layout with its own Kotlin class and have the class implement the interface instead. This might get tedious if you have lots of such layouts, so "pick your poison", this is just another idea.
Last, see @Daniel Wilson's answer. It avoids the compiler error and makes you specify the namespace for which ivFoodImage
you want to use.
Referencing this answer, you can specifically import the ID you want and name it using Kotlin's as
keyword
package XXX
import kotlinx.android.synthetic.main.num_info_inet_plus_pack.view.circle as inetViewCircle
import kotlinx.android.synthetic.main.num_info_pack.view as circle
//...
val inetView = activity.layoutInflater.inflate(R.layout.num_info_pack, parent, false)
inetViewCircle.setBackgroundResource(background)