kotlin中的过载分辨率模糊错误

MB_*_*der 32 xml android android-layout kotlin

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

这是一个主要的活动.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
    }

    }
 }
Run Code Online (Sandbox Code Playgroud)

这是一个布局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>
Run Code Online (Sandbox Code Playgroud)

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> 
Run Code Online (Sandbox Code Playgroud)

Les*_*Les 51

ivFoodImage在两个布局中定义.而你正在导入他们的定义......

import kotlinx.android.synthetic.main.activity_food_details.view.*
import kotlinx.android.synthetic.main.food_ticket.view.*
Run Code Online (Sandbox Code Playgroud)

考虑更改其中一个布局中的名称,或者在定义中明确更改名称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` 在这里会很有用。 (2认同)

Dan*_*son 8

引用此答案,您可以专门导入所需的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)
Run Code Online (Sandbox Code Playgroud)