Search code examples
androidkotlinandroid-context

Android Kotlin - Classifier 'Activity' does not have a companion object, and thus must be initialized here


        var acct = Activity

        when {
            activityName.contains("MainActivity", true) -> {
                acct = (context as MainActivity)

            }
            activityName.contains("UserMemes", true) -> {
                acct = (context as UserMemes)

            }
            activityName.contains("SearchShow", true) -> {
                acct = (context as SearchShow)

            }
        }

        acct.memeIdAndLastPos[meme.id] = scrollY

I get Classifier 'Activity' does not have a companion object, and thus must be initialized here On Activity

When I do Activity() instead then the error disappears but I can't use memeIdAndLastPos any more.

How to fix?


Solution

  • val acct: Activity = when { 
      activityName.contains("MainActivity", true) ->  context as MainActivity
      activityName.contains("UserMemes", true) ->  context as UserMemes
      activityName.contains("SearchShow", true) -> context as SearchShow  
    }
    
    acct.memeIdAndLastPos[meme.id] = scrollY