首页 > 解决方案 > 并且它的超类没有带有 Kotlin 和 DI 的 @Subscribe 注释的公共方法

问题描述

我已经检查了堆栈上的所有答案。但没有任何帮助。可能是 Kotlin + DI 的问题

所以我得到了一个例外,即 Eventbus 出于某种原因无法在演示者类中初始化自身。通过调试器,我看到从 onCreate 以正确方式执行的初始化方法。我使用与在其他类(Java 类)中使用的代码相同的代码,并且其他代码正常工作。你能否给一个建议,可能是一个问题

错误

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)

我的主讲课

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)
    }

}

标签: androidkotlinevent-bus

解决方案


MyProfileActivity 及其超类没有带有 @Subscribe 注释的公共方法

强调我的。private在此处从您的函数中删除:

@Subscribe
private fun updateActionsCount(event: VisitsEvent)

推荐阅读