提问人:R3inbow 提问时间:12/12/2022 更新时间:12/12/2022 访问量:472
未解析的引用 [KOTLIN]
Unresolved Reference [KOTLIN]
问:
我正在尝试将变量从一个类发送到在另一个包中创建的另一个类,但它一直告诉我我有未解析的引用,我无法导入该变量。
这是我尝试调用变量的地方(我说的是“KEY_ACCOUNT_LIST_CHANGED”和“KEY_CURRENT_ACCOUNT_CHANGED”):
if (requestCode == ACTION_MANAGE_ACCOUNTS && resultCode == RESULT_OK && data!!.getBooleanExtra(
AccountsManagementActivity.KEY_ACCOUNT_LIST_CHANGED,
false
)
) {
// current account has changed
if (data.getBooleanExtra(AccountsManagementActivity.KEY_CURRENT_ACCOUNT_CHANGED, false)) {
account = drawerViewModel.getCurrentAccount(this)
// Refresh dependencies to be used in selected account
initDependencyInjection()
restart()
}
正如你所看到的,它们在类“AccountsManagementActivity”中被摧毁,该类是这样构建的:
abstract class AccountsManagementActivity : FileActivity(), AccountsManagementAdapter.AccountAdapterListener, AccountManagerCallback<Boolean> {
val KEY_CURRENT_ACCOUNT_CHANGED = "CURRENT_ACCOUNT_CHANGED"
val KEY_ACCOUNT_LIST_CHANGED = "ACCOUNT_LIST_CHANGED"
....
我试图从 AccountsManagementActivity 中删除“摘要”,然后在另一个类中进行以下调用:
val accountsManagementActivity = AccountsManagementActivity()
但应用程序崩溃了
我希望有人能帮助我
答:
1赞
Rahim Virani
12/12/2022
#1
如果你想访问这样的变量,你需要使它们是静态的。 对于 kotlin,您需要
companion object {
val KEY_CURRENT_ACCOUNT_CHANGED = "CURRENT_ACCOUNT_CHANGED"
val KEY_ACCOUNT_LIST_CHANGED = "ACCOUNT_LIST_CHANGED"
}
现在您可以像这样访问它们
AccountsManagementActivity.KEY_ACCOUNT_LIST_CHANGED
评论