简体   繁体   中英

Unresolved Reference [KOTLIN]

I'm trying to send a variable from one class to another class created in a different package but it keeps telling me that i have unresolved reference and i can't import the variable.

This is where i try to call the variables (The ones i'm talking about are "KEY_ACCOUNT_LIST_CHANGED" and "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()
    }

As you can see, they are declaired at the class "AccountsManagementActivity" which is build this way:

abstract class AccountsManagementActivity : FileActivity(), AccountsManagementAdapter.AccountAdapterListener, AccountManagerCallback<Boolean> {

    val KEY_CURRENT_ACCOUNT_CHANGED = "CURRENT_ACCOUNT_CHANGED"
    val KEY_ACCOUNT_LIST_CHANGED = "ACCOUNT_LIST_CHANGED"

....

I've tried to delete the "abstract" from AccountsManagementActivity and then in the other class make this call:

val accountsManagementActivity = AccountsManagementActivity()

but the app crashes

i hope anyone can help me

if you want to access variables like that you need to make them static. For kotlin you need

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

now you can access them like this

AccountsManagementActivity.KEY_ACCOUNT_LIST_CHANGED

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM