Overload Resolution Ambiguity error in kotlin
How do I fix this Overload error, I have Overload Resolution Ambiguity error, I sync it in my project and clean it and rebuild it but it's get me bellow error ,I add main activity code in kotlin with 2 layout activity Here is a photo of the error
Here is a main activity.kt
package com.hussein.startup
import android.content.Context
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import kotlinx.android.synthetic.main.activity_food_details.view.*
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.food_ticket.view.*
class MainActivity : AppCompatActivity() {
var adapter:FoodAdapter?=null
var listOfFoods =ArrayList<Food>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// load foods
listOfFoods.add(Food("Coffee"," Coffee preparation is",R.drawable.a))
.....
gvListFood.adapter =adapter
}
class FoodAdapter:BaseAdapter {
var listOfFood= ArrayList<Food>()
var context:Context?=null
constructor(context:Context,listOfFood:ArrayList<Food>):super(){
this.context=context
this.listOfFood=listOfFood
}
override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
val food = this.listOfFood[p0]
var inflator = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
var foodView= inflator.inflate(R.layout.food_ticket,null)
foodView.ivFoodImage.setImageResource(food.image!!)
foodView.ivFoodImage.setOnClickListener {
val intent = Intent(context,FoodDetails::class.java)
intent.putExtra("name",food.name!!)
intent.putExtra("des",food.des!!)
intent.putExtra("image",food.image!!)
context!!.startActivity(intent)
}
foodView.tvName.text = food.name!!
return foodView
}
override fun getItem(p0: Int): Any {
return listOfFood[p0]
}
override fun getItemId(p0: Int): Long {
return p0.toLong()
}
override fun getCount(): Int {
return listOfFood.size
}
}
}
Here is a layout xml
1-activity_food_details.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FoodDetails">
<ImageView
android:id="@+id/ivFoodImage"
android:layout_width="50pt"
android:layout_height="50pt"
android:layout_marginTop="52dp"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/c"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
app:layout_constraintHorizontal_bias="0.501" />
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="TextView"
android:textColor="@color/colorPrimary"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="48dp"
app:layout_constraintTop_toBottomOf="@+id/ivFoodImage" />
<TextView
android:id="@+id/tvDetails"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="56dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvName" />
</android.support.constraint.ConstraintLayout>
2-food_ticket.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="63pt"
android:layout_height="wrap_content"
android:background="@color/gray"
android:orientation="vertical"
android:padding="3pt">
<LinearLayout
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical">
<ImageView
android:id="@+id/ivFoodImage"
android:layout_width="50pt"
android:layout_height="50pt"
app:srcCompat="@drawable/c" />
<TextView
android:id="@+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Coffe"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
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)
This means that resource ids in java files from xml files are not properly imported, or are imported with wrong xml resource id files because of same name.
Suppose for
----activity_login
----activity_main
there is a textview with same id.
Kotlin imports tries to search every xml file ids, and the id gets wrongly imported.
Solution:: Remove all imports after copy/paste and follow alt+enter
one by one.