史上最简洁Kotlin版EventBus的使用教程

EventBus简介

  • EventBus是一种用于Android的事件发布-订阅总线。他简化了应用程序内各个组件之间进行通信的复杂度。

GitHub - greenrobot/EventBus: Event bus for Android and Java that simplifies communication between Activities, Fragments, Threads, Services, etc. Less code, better quality.icon-default.png?t=M5H6https://github.com/greenrobot/EventBus

EventBus使用步骤 

        1、配置gradle,导入依赖

implementation 'org.greenrobot:eventbus:3.3.1'

        2、定义Event类型(非必须

package com.example.eventbusdemo

data class MessageEvent(val name: String) {

}

        3、注册、注销EventBus

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //注册订阅者
        //避免重复注册,重复注册会导致崩溃
        if (!EventBus.getDefault().isRegistered(this)) { //这里的取反别忘记了
            EventBus.getDefault().register(this)
        } else {
            println("请勿重复注册事件")
        }
    }
    override fun onDestroy() {
        super.onDestroy()
        //注销订阅者
        EventBus.getDefault().unregister(this)
    }

        4、定义事件处理订阅者

    //准备接受事件的订阅者
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: MessageEvent) {
        println(event.name)
    }

        5、创建并发送消息

    fun sendEvent(v: View) {
        EventBus.getDefault().post(MessageEvent("我来自第二个页面"))
    }

运行效果展示

常见问题总结

1、Caused by: org.greenrobot.eventbus.EventBusException: Subscriber class com.example.eventbusdemo.SecondActivity and its super classes have no public methods with the @Subscribe annotation

Caused by: org.greenrobot.eventbus.EventBusException: Subscriber class com.example.eventbusdemo.SecondActivity 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:150)
        at com.example.eventbusdemo.SecondActivity.onCreate(SecondActivity.kt:20)
        at android.app.Activity.performCreate(Activity.java:7893)
        at android.app.Activity.performCreate(Activity.java:7880)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3313)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3487) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2071) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:224) 
        at android.app.ActivityThread.main(ActivityThread.java:7561) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:995) 

问题原因:你这个类注册了 EventBus 可是这个类没有写带 @Subscribe 注解的方法

解决方法:补上该方法即可


2、D/EventBus: No subscribers registered for event class com.example.eventbusdemo.MessageEvent
      D/EventBus: No subscribers registered for event class org.greenrobot.eventbus.NoSubscriberEvent

D/EventBus: No subscribers registered for event class com.example.eventbusdemo.MessageEvent
D/EventBus: No subscribers registered for event class org.greenrobot.eventbus.NoSubscriberEvent

问题原因:EventBus没有被注册上,检查 EventBus 的注册代码

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

super码王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值