Search code examples
androidkotlinevent-bus

and its super classes have no public methods with the @Subscribe annotation with Kotlin and DI


I have checked all answers on the stack. But nothing helps. Might be problem in Kotlin + DI

So I got an exception that Eventbus for some reason cant initialize itself in presenter class. By debugger I see that method of initializing executed proper way from onCreate. I use the same code as I use in other classes (Java classes) and other works correctly. Could you please give an advice where might be an issue

Error

Caused by: org.greenrobot.eventbus.EventBusException: Subscriber class com.myapp.mvp.ui.myprofile.MyProfileActivity and its super classes have no public methods with the @Subscribe annotation
    at org.greenrobot.eventbus.SubscriberMethodFinder.findSubscriberMethods(SubscriberMethodFinder.java:67)
    at org.greenrobot.eventbus.EventBus.register(EventBus.java:140)
    at com.myapp.mvp.ui.myprofile.MyProfileActivity.onCreate(MyProfileActivity.kt:67)
    at android.app.Activity.performCreate(Activity.java:7981)
    at android.app.Activity.performCreate(Activity.java:7970)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)

My presenter class

import com.arellomobile.mvp.InjectViewState
import com.arellomobile.mvp.MvpPresenter
import com.myapp.UserProfileItemsList
import com.myapp.eventbus.VisitsEvent
import com.myapp.eventbus.UserEvent
import com.myapp.models.UserDescriptionModel
import com.myapp.mvp.model.interactor.myprofile.MyProfileInteractor
import com.myapp.utils.ActionsCountInfoCallback
import com.myapp.utils.UserCallback
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe

import java.util.ArrayList
import javax.inject.Inject

@InjectViewState
class MyProfilePresenter @Inject constructor(private val interactor: MyProfileInteractor) : MvpPresenter<MyProfileView>() {
    private val userDescriptionList = ArrayList<UserDescriptionModel>()

    override fun onFirstViewAttach() {
        super.onFirstViewAttach()
        setAllCurrentUserInfo()
        setActionsCount()
    }

    fun setAllCurrentUserInfo() {
        interactor.getAllCurrentUserInfo(UserCallback{ fsUser ->
            viewState.setUserData(fsUser.name, fsUser.age, fsUser.country, fsUser.image)
            userDescriptionList.addAll(UserProfileItemsList.initData(fsUser))
            viewState.setList(userDescriptionList)
            EventBus.getDefault().post(UserEvent(fsUser))
        })
    }

    private fun setActionsCount() {
        interactor.getActionsCountInfo(
                ActionsCountInfoCallback{ visits, likes -> viewState.setActionsCount(visits, likes) })
    }

    @Subscribe
    private fun updateActionsCount(event: VisitsEvent){
        viewState.setActionsCount(event.getmVisits(), event.getmLikes())
    }


    fun registerSubscribers() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
    }

    fun unsubscribe(){
        EventBus.getDefault().unregister(this)
    }

}

Solution

  • MyProfileActivity and its super classes have no public methods with the @Subscribe annotation

    Emphasis mine. Remove the private from your function here:

    @Subscribe
    private fun updateActionsCount(event: VisitsEvent)