【问题标题】:Overload Resolution Ambiguity error in kotlinkotlin 中的重载分辨率歧义错误
【发布时间】:2018-02-13 04:23:51
【问题描述】:

我该如何解决这个过载错误,我有过载分辨率歧义错误,我在我的项目中同步它并清理它并重建它,但它让我出现以下错误,我在 kotlin 中添加了具有 2 个布局活动的主要活动代码 这是错误的照片

这是一个主要的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> 

【问题讨论】:

    标签: android xml android-layout kotlin


    【解决方案1】:

    您在两种布局中都定义了ivFoodImage。你正在像这样导入他们的定义......

    import kotlinx.android.synthetic.main.activity_food_details.view.*
    import kotlinx.android.synthetic.main.food_ticket.view.*
    

    考虑在其中一种布局中更改名称,或者在 foodView 的定义中明确说明,或者使用 activity_food_details 删除导入(如果不使用)。

    编辑

    澄清可能的解决方案...

    1. “更改名称” - 在您的一个布局中,将 ivFoodImage 更改为其他名称,例如 ivFoodImage_Details。
    2. “删除未使用的导入” - 不言自明,是一种很好的做法。
    3. “显式” - 删除您想要显式使用的导入,然后执行 OP 正在执行的操作,即 var foodView = inflator.inflate(R.layout.food_ticket,null),在这种情况下从 food_ticket 显式加载。

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

    这里还有另一种选择。如果您试图让布局实现某种“接口”,请考虑使用其自己的 Kotlin 类包装每个布局,并让该类实现接口。如果你有很多这样的布局,这可能会变得乏味,所以“挑选你的毒药”,这只是另一个想法。

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

    【讨论】:

    • 有趣。那么没有优雅的方法可以在布局 xml 中为视图提供相同的 id 吗?我认为拥有多个具有相同 id 的相似视图和 findViewById() 不在乎 id 来自哪里仍然有用。也许typealias 在这里有用。
    • 嗨,伙计,和你一样......谢谢你节省了我的时间
    • 有人可以告诉我如何明确说明 foodView 的定义,如答案中所述吗?
    • @Les 在使用 ConstraintLayout 并使用 ID 为 stackoverflow.com/questions/43676415/… 的布局标签时出现此错误
    【解决方案2】:

    Referencing this answer,你可以专门导入你想要的ID,并使用Kotlin的as关键字命名

    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)
    

    【讨论】:

    • 我很高兴 Kotlin 让我免于编写所有这些样板代码!
    【解决方案3】:

    这意味着xml文件中java文件中的资源id没有正确导入,或者因为同名而导入了错误的xml资源id文件。

    假设为

    ----activity_login 
    ----activity_main 
    

    有一个 id 相同的 textview。

    Kotlin 导入尝试搜索每个 xml 文件 id,但 id 被错误地导入。

    解决方案:: 复制/粘贴后删除所有导入,并一一关注alt+enter。

    【讨论】:

      【解决方案4】:

      @DanielWilson 的回答是正确的。如果您有 2 个相似的布局,则无需重命名相同的字段以使其唯一。

      但是您必须一一导入所有相等的字段并重命名它们。因此,如果您没有在 in layouts 中重命名,您将在 in code 中重命名它们。例如,

      import kotlinx.android.synthetic.main.row_profile_balance_refill.amount as refill_amount
      import kotlinx.android.synthetic.main.row_profile_balance_refill.reason as refill_reason
      import kotlinx.android.synthetic.main.row_profile_balance_withdrawal.amount as withdrawal_amount
      import kotlinx.android.synthetic.main.row_profile_balance_withdrawal.reason as withdrawal_reason
      

      我遇到了 Kotlin 无法解析哪个字段对应哪个布局的情况。

      奇怪,但我不能使用refill_amount 和refill_reason。然后我使用了旧的 Java 方法findViewById()。所以,图片上的一个类变成了:

      class RefillViewHolder(itemView: View) : AbstractViewHolder(itemView) {
          val amount: TextView = itemView.findViewById(R.id.amount)
          val reason: TextView = itemView.findViewById(R.id.reason)
      }
      

      【讨论】:

        【解决方案5】:

        根据上面 Les 的回答,我通常喜欢保持简单的命名约定,例如调用 RecyclerView 的 id @+id/recyclerView。如果我有一个名为 ExampleActivity.java 且布局为 R.layout.activity_example 的 Activity,我不想直接导入该布局中的每个视图,我宁愿只导入布局中的所有视图。所以我只需在活动中导入整个文件:

        import kotlinx.android.synthetic.main.activity_example.*
        

        从我的活动布局文件中导入所有视图,因为无论如何我通常都会访问所有视图。如果您的布局文件包含其他布局,您也必须单独导入这些布局。因此,如果我使用我的 activity_example.xml 文件中包含的标题布局,我会导入整个布局文件

        import kotlinx.android.synthetic.main.header_layout.*
        

        【讨论】:

          猜你喜欢
          • 2019-12-16
          • 1970-01-01
          • 1970-01-01
          • 2019-12-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多