我该如何解决这个过载错误,我有过载分辨率歧义错误,我在我的项目中同步它并清理它并重建它,但它让我出现以下错误,我在 kotlin 中添加了具有 2 个布局 Activity 的主要 Activity 代码 这是错误的照片
这是一个主要的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
}
}
}
这是一个布局 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>
最佳答案
您正在定义 ivFoodImage
在您的两种布局中。你正在像这样导入他们的定义......
import kotlinx.android.synthetic.main.activity_food_details.view.*
import kotlinx.android.synthetic.main.food_ticket.view.*
考虑在其中一种布局中更改名称,或在 foodView
的定义中明确说明,或使用 activity_food_details
删除导入如果它没有被使用。
编辑
澄清可能的解决方案...
- “更改名称” - 在您的一个布局中,将 ivFoodImage 更改为其他名称,例如
ivFoodImage_Details
. - “删除未使用的导入” - 不言自明,是一种很好的做法。
- “显式” - 删除您想要显式使用的导入,然后执行 OP 正在执行的操作,即
var foodView = inflator.inflate(R.layout.food_ticket,null)
,从food_ticket
显式加载在这种情况下。
在多个布局中使用相同名称的概念还不错(考虑接口(interface)和注入(inject))。但是 kotlinx.android.synthetic
是一种语法糖,可以使事情变得不那么冗长。它妨碍了这里的目标。
这里还有另一种选择。如果您试图让布局实现某种“接口(interface)”,请考虑使用其自己的 Kotlin 类包装每个布局,并让该类实现接口(interface)。如果你有很多这样的布局,这可能会变得乏味,所以“挑选你的毒药”,这只是另一个想法。
最后,请参阅@Daniel Wilson 的回答。它避免了编译器错误并让您指定 ivFoodImage
的 namespace 。你想用。
关于android - kotlin 中的重载解析歧义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46045377/