使用EventBus 3.0 报 Subscriber class com.example.test.MainActivity and its super classes have no public methods with the @Subscribe annotation

简介: 使用EventBus 3.0 报 Subscriber class com.example.test.MainActivity and its super classes have no public methods with the @Subscribe annotation

使用EventBus 3.0 报 Subscriber class com.example.test.MainActivity and its super classes have no public methods with the @Subscribe annotation


代码如下:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //订阅事件
        EventBus.getDefault().register(this);
        
        EventBus.getDefault().post(new MessageEvent("onCreate",0));
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //取消订阅
        EventBus.getDefault().unregister(this);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void whenOnClick(){
        Toast.makeText(this, "收到事件", Toast.LENGTH_SHORT).show();
    }
}

原因:被注解 @Subscribe 标注的方法 参数没有对应的Event类型

解决方法:

方法参数加上对应的Event类型 参数的类型和发送的类型对应,当发送了多个类型时,注解方法只接收并处理与其发送的Event类型一致的事件

@Subscribe(threadMode = ThreadMode.MAIN)
    public void whenOnClick(MessageEvent messageEvent){
        Toast.makeText(this, "收到事件", Toast.LENGTH_SHORT).show();
    }

粘性事件

发送事件后再订阅事件也可以收到,发送时使用postSticky 方法

EventBus.getDefault().postSticky(new MessageEvent("onCreate",0));

需要指定 sticky = true  默认为false

@Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void whenOnClickSticky(MessageEvent messageEvent){
        Toast.makeText(this, "收到了事件"+messageEvent.getMessage(), Toast.LENGTH_SHORT).show();
    }

事件优先级

高优先级的事件优先于低优先级的事件,不管高低优先级,都会收到事件并处理,指定事件优先级使用  @Subscribe 的priority指定即可

@Subscribe(threadMode = ThreadMode.MAIN,priority = 2)

 

目录
相关文章
EventBus: Could not dispatch event: class com.********.LoginEvent to subscribing class
Could not dispatch event 04-18 14:10:11.062 4790-4790/com. E/EventBus: Could not dispatch event: class com.
8635 0
【Junit 报错】Test class should have exactly one public zero-argument constructor和Test class can only have one constructor
错误1: 1 java.lang.Exception: Test class should have exactly one public zero-argument constructor 2 at org.
6184 0
|
前端开发 开发者
class-constructor 构造器中 super 函数的使用说明|学习笔记
快速学习 class-constructor 构造器中 super 函数的使用说明
123 0
|
Android开发 开发者
【解决问题的思路】its super classes have no public methods with the @Subscribe annotation
【解决问题的思路】its super classes have no public methods with the @Subscribe annotation
1082 0
Kyro - To register this class use: kryo.register
Kyro - To register this class use: kryo.register
350 0
|
前端开发
SpringMVC - Failed to instantiate Specified class is an interface
SpringMVC - Failed to instantiate Specified class is an interface
595 0
|
Java
【EventBus】Subscribe 注解分析 ( Subscribe 注解属性 | threadMode 线程模型 | POSTING | MAIN | MAIN_ORDERED | ASYNC)
【EventBus】Subscribe 注解分析 ( Subscribe 注解属性 | threadMode 线程模型 | POSTING | MAIN | MAIN_ORDERED | ASYNC)
740 0
Object C学习笔记18-SEL,@ selector,Class,@class
  本章是对上一章的一点补充,所以比较简单点。     一. SEL 类型     在上一篇介绍了几个方法,都只是介绍了其使用方式但是没有具体介绍参数: - (id)performSelector:(SEL)aSelector; - (id)performSelector:(SEL)a...
965 0
|
Kotlin 容器
EventBus3.1.1 解决 Caused by: org.greenrobot.eventbus.EventBusException: Subscriber class ... and its super classes have no public methods with the @Sub
      小菜今天自己写测试 Demo 时,需要用到 EventBus,目前集成 3.1.1 版本,集成的方式很简单,在某个 Fragment 实践应用中,却一直报入下错: Caused by: org.
12850 0