Overload Resolution Ambiguity error in kotlin

前端 未结 5 485

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

相关标签:
5条回答
  • 2021-01-01 09:08

    @DanielWilson answer is correct. If you have 2 similar layouts, you don't need to rename equal fields in order to make them unique.

    But you have to import all equal fields one-by-one renaming them. So, if you haven't renamed in layouts, you would rename them in code. For instance,

    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
    

    I got into situation where Kotlin couldn't resolve which field corresponds to which layout.

    Strange, but I couldn't use refill_amount and refill_reason. Then I used old Java method findViewById(). So, a class at the picture turns to:

    class RefillViewHolder(itemView: View) : AbstractViewHolder(itemView) {
        val amount: TextView = itemView.findViewById(R.id.amount)
        val reason: TextView = itemView.findViewById(R.id.reason)
    }
    
    0 讨论(0)
  • 2021-01-01 09:15

    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)
    
    0 讨论(0)
  • 2021-01-01 09:15

    Building off of Les' answer above, I usually like keeping my naming conventions simple like calling a RecyclerView's id @+id/recyclerView. If I have an Activity called ExampleActivity.java with layout R.layout.activity_example, I don't want to directly import every single view in that layout, I'd rather just import all the views from the layout. So I'd just import the whole file in the activity:

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

    to import all the views from my activity's layout file since I usually access all of them anyways. If your layout file includes other layouts, you'll have to import those layouts separately too. So if i use a header layout included in my activity_example.xml file, i'd import that whole layout file

    import kotlinx.android.synthetic.main.header_layout.*
    
    0 讨论(0)
  • 2021-01-01 09:20

    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.

    0 讨论(0)
  • 2021-01-01 09:27

    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...

    1. "Changing the name" - in one of your layouts, change ivFoodImage to something else like 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.

    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.

    0 讨论(0)
提交回复
热议问题