使用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)

 

目录
相关文章
|
Java Android开发
The method call() of type XXX must override a superclass
The method call() of type XXX must override a superclass
112 0
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.
8627 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.
6172 0
abstract class和interface有什么区别?
声明方法的存在而不去实现它的类被叫做抽象类(abstract class)
105 0
|
Android开发 Kotlin
【错误记录】Kotlin 编译报错 ( Class ‘Xxx‘ is not abstract and does not implement abstract member )
【错误记录】Kotlin 编译报错 ( Class ‘Xxx‘ is not abstract and does not implement abstract member )
781 0
【错误记录】Kotlin 编译报错 ( Class ‘Xxx‘ is not abstract and does not implement abstract member )
|
Android开发 开发者
【解决问题的思路】its super classes have no public methods with the @Subscribe annotation
【解决问题的思路】its super classes have no public methods with the @Subscribe annotation
1070 0
|
Java
【EventBus】Subscribe 注解分析 ( Subscribe 注解属性 | threadMode 线程模型 | POSTING | MAIN | MAIN_ORDERED | ASYNC)
【EventBus】Subscribe 注解分析 ( Subscribe 注解属性 | threadMode 线程模型 | POSTING | MAIN | MAIN_ORDERED | ASYNC)
733 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.
12834 0