【问题标题】:and its super classes have no public methods with the @Subscribe annotation with Kotlin and DI并且它的超类没有带有 Kotlin 和 DI 的 @Subscribe 注释的公共方法
【发布时间】:2020-02-26 21:06:55
【问题描述】:

我已经检查了堆栈上的所有答案。但没有任何帮助。可能是 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)
    }

}

【问题讨论】:

    标签: android kotlin event-bus


    【解决方案1】:

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

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

    @Subscribe
    private fun updateActionsCount(event: VisitsEvent)
    

    【讨论】:

    • 它的 Kotlin 没有公共/私有。你说的是哪种方法?
    • 谢谢你。是的,我错过了。它有帮助。我在想注册事件总线的方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 2014-02-21
    相关资源
    最近更新 更多