• 史上最简洁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类型(非必须

    1. package com.example.eventbusdemo
    2. data class MessageEvent(val name: String) {
    3. }

            3、注册、注销EventBus

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

            4、定义事件处理订阅者

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

            5、创建并发送消息

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

    运行效果展示

    常见问题总结

    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

    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
    2. at org.greenrobot.eventbus.SubscriberMethodFinder.findSubscriberMethods(SubscriberMethodFinder.java:67)
    3. at org.greenrobot.eventbus.EventBus.register(EventBus.java:150)
    4. at com.example.eventbusdemo.SecondActivity.onCreate(SecondActivity.kt:20)
    5. at android.app.Activity.performCreate(Activity.java:7893)
    6. at android.app.Activity.performCreate(Activity.java:7880)
    7. at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
    8. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3313)
    9. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3487
    10. at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83
    11. at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135
    12. at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95
    13. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2071
    14. at android.os.Handler.dispatchMessage(Handler.java:107
    15. at android.os.Looper.loop(Looper.java:224
    16. at android.app.ActivityThread.main(ActivityThread.java:7561
    17. at java.lang.reflect.Method.invoke(Native Method) 
    18. at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539
    19. 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

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

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

  • 相关阅读:
    Redis持久化
    .NET静态代码织入——肉夹馍(Rougamo)发布3.0
    C语言-操作符详解
    Unity ECS实例:制作俯视角射击游戏!
    视频剪辑素材哪里找?这个几个网站就够了。
    游戏开发的魔法之笔:建造者设计模式的崭新艺术
    动态SQL
    神经网络预测指标是什么,神经网络怎么预测数据
    6、STL、迭代器、容器
    什么是RESTful API,以及如何它使用构建 web 应用程序(InsCode AI 创作助手)
  • 原文地址:https://blog.csdn.net/qq_41885673/article/details/125420626