首页 > 解决方案 > Function unresolved reference in main activity

问题描述

so I'll try to explain my problem this time without pasting you 400 lines of code.

I have a custom view that im using in my main activity (com.company.app.common.ui.view1), in my main activity it's imported properly as follow import com.company.app.common.ui.view1

Inside view1 i have a function defined in the constructor fun updateItems(items: List<Item>){}

Item is an object I have modeled properly and the function has no errors.

But when I try to call it in my main activity using view1.updateItems(items) I get an unresolved reference on updateItems.

the function is supposedly public by default if im not wrong, can anyone help? Thanks

标签: androidkotlinandroidx

解决方案


看起来您正在尝试在类而不是实例上调用该方法(获取实例的一种方法是通过构造函数创建它myView1 = view1(...),然后您可以在其上调用该方法myView1.updateItems(items)

还有两点:

  • 无需发布所有代码,只需发布​​片段(仅与问题相关的代码),将不相关的代码替换为...并保留括号
  • 在我的回答中,我假设(根据你写的)view1应该是一个类/对象,最好将它的名字大写,这样它就不会与实例混淆

推荐阅读