我如何修复这个重载错误,我有重载解析模糊错误,我在我的项目中同步它,清理并重建它,但它是get me bellow错误,我在kotlin中添加了2个布局活动的主活动代码

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

为了阐明可能的解决方案...

  1. "更改名称"-在您的一个布局中,将ivFoodImage更改为其他名称,如ivFoodImage_Details.
  2. "removing the unused import" - self explanatory and a good practice.
  3. "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 from food_ticket in this case.

在多个布局中使用相同名称的概念不错(考虑接口和注入).但kotlinx.android.synthetic是一种语法糖果,可以让事情变得不那么冗长.它在这里阻碍了进球.

这里还有另一种 Select .如果您试图让布局实现一种"接口",请考虑用自己的KOTLIN类包装每个布局,并让类实现接口.如果你有很多这样的布局,这可能会变得乏味,所以" Select 你的毒药",这只是另一个 idea .

最后,请看@Daniel Wilson的回答.它避免了编译器错误,并让您指定要使用ivFoodImage的命名空间.

Kotlin相关问答推荐

如何在Kotlin中为两个数据类创建可重用的方法?

在kotlin中使用List(mylist. size){index—TODO()}或Map迭代>

在 Kotlin 中定义基于多态函数的泛型函数

为什么 Kotlin 中没有 init 块的注释

如何使用multiset与JOOQ获取关联的记录列表?

如何使用 Kotlin Maven 更改 Minecraft 插件中的 Shulker GUI 标题

在协程上下文中重新抛出异常

将一个列表元素分组为多个组(indexBy)

如何在 micronaut 上启用 swagger UI?

顶级属性的初始化

你怎么知道什么时候需要 yield()?

如何在 Kotlin for Android 上使用setTextColor(hexaValue),

Kotlin:泛型、反射以及类型 T 和 T:Any 之间的区别

如何使 TextInputEditText 只读?

将 jetpack compose 添加到现有元素

无法在 kotlin android 中以编程方式禁用 EditText

如何将map函数应用于Kotlin中的数组并更改其值?

Kotlin:使用自定义设置器时没有lateinit的解决方法?

在android的默认浏览器 Select 列表中添加我的浏览器?

在android java类中使用Kotlin扩展