首页 > 其他分享 >史上最简洁Kotlin版EventBus的使用教程

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

时间:2022-12-16 10:38:12浏览次数:55  
标签:教程 java Kotlin app greenrobot EventBus com android


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'

非必须)

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("我来自第二个页面"))
}

运行效果展示

史上最简洁Kotlin版EventBus的使用教程_开发语言

常见问题总结

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 的注册代码

标签:教程,java,Kotlin,app,greenrobot,EventBus,com,android
From: https://blog.51cto.com/u_15361941/5946865

相关文章

  • Spring Boot + vue-element 开发个人博客项目实战教程(二十一、个人介绍、公告管理、标
    ⭐作者简介:码上言⭐代表教程:​​SpringBoot+vue-element开发个人博客项目实战教程​​⭐专栏内容:零基础学Java、个人博客系统文章目录​​一、个人介绍​​​​二、......
  • 图书推荐:Kotlin从入门到进阶实战
    图片发自简书App《Kotlin从入门到进阶实战》从Kotlin语言的基础语法讲起,逐步深入到Kotlin进阶实战,并在最后配合项目实战案例,重点介绍了使用Kotlin+SpringBoot......
  • IDEA入门级使用教程----你怎么还在用eclipse?
    上个月,idea的使用量超越eclipse的消息席卷了整个IT界,idea到底好在哪里呢?最智能的IDEIDEA相对于eclipse来说最大的优点就是它比eclipse聪明。聪明到什么程度呢?我们先来看几个......
  • UML类图新手教程,看完这篇你就会了
    第一篇UML类图新手入门级介绍举一个简单的例子,来看这样一副图,其中就包括了UML类图中的基本图示法。1首先,看动物矩形框,它代表一个类(Class)。类图分三层,第一层显示类的名称,如......
  • RNA-seq 详细教程:Wald test(10)
    学习目标了解生成比较结果所需的步骤(Wald检验)总结不同层次的基因过滤了解对数倍变化收缩结果探索默认情况下,DESeq2使用Wald检验来识别在两个样本之间差异表达的......
  • gtest学习教程(从0到1)
    gtest使用教程1简介之前对gtest一无所知,最近,找了些相关的资料,学习了下.这里主要记录了学习过程和相关知识点.什么是gtest:gtest测试框架是在不同平台上(Linux,MacOS......
  • DRF教程文档解析(一)
    序列化器参考网址https://blog.csdn.net/weixin_35688430/article/details/111203136定义序列化器#modelsclassBookInfo(models.Model):btitle=models......
  • 离线安装mysql5.7【教程二】
    openjdk1.8文件下载链接地址:链接:https://pan.baidu.com/s/1fLt_dNILuw5VsV_PcA_yAQ提取码:ot3emysql5.7安装包下载链接地址:链接:https://pan.baidu.com/s/1du7Dk7WtJ0Ac......
  • 「Docker学习系列教程」基础篇小总结及高级篇预告
    通过前面十来篇的学习,我们已经把docker基础篇学习完了。这篇文章,咱们就来小总结下基础篇学习的东西以及介绍接下来高级篇中,将会学习到哪些知识点。 基础篇总结:第一篇,......
  • opencl 教程
    这是第一篇真正的OpenCL教程。这篇文章不会从GPU结构的技术概念和性能指标入手。我们将会从OpenCL的基础API开始,使用一个小的kernel作为例子来讲解基本的计算管理。首先我......