未解析的指涉[Kotlin]

ws51t4hk  于 2022-12-13  发布在  Kotlin
关注(0)|答案(1)|浏览(163)

我试图将一个变量从一个类发送到另一个在不同包中创建的类,但它一直告诉我,我有未解析的引用,我不能导入变量。
这是我尝试调用变量的地方(我所说的变量是“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中删除“abstract”,然后在另一个类中进行以下调用:

val accountsManagementActivity = AccountsManagementActivity()

但应用程序崩溃了
我希望任何人都能帮助我

vqlkdk9b

vqlkdk9b1#

如果你想访问这样的变量,你需要使它们成为静态的。

companion object {
    val KEY_CURRENT_ACCOUNT_CHANGED = "CURRENT_ACCOUNT_CHANGED"
    val KEY_ACCOUNT_LIST_CHANGED = "ACCOUNT_LIST_CHANGED"
}

现在您可以通过以下方式访问它们

AccountsManagementActivity.KEY_ACCOUNT_LIST_CHANGED

相关问题