栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

史上最简洁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.https://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 的注册代码

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/993541.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号