使用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 Spring
【新手指南】严重: Exception sending context initialized event to listener instance of class
【新手指南】严重: Exception sending context initialized event to listener instance of class
506 0
【新手指南】严重: Exception sending context initialized event to listener instance of class
|
Java Android开发
The method call() of type XXX must override a superclass
The method call() of type XXX must override a superclass
104 0
class_destroy源码分析
class_destroy源码分析
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.
8605 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.
6152 0
|
Java Apache Spring
Exception sending context initialized event to listener instance of class
详细错误信息如下: 严重: Exception sending context initialized event to listener instance of class com.auth.spring.
1221 0
|
Android开发 开发者
【解决问题的思路】its super classes have no public methods with the @Subscribe annotation
【解决问题的思路】its super classes have no public methods with the @Subscribe annotation
1022 0
Kyro - To register this class use: kryo.register
Kyro - To register this class use: kryo.register
326 0
|
前端开发
SpringMVC - Failed to instantiate Specified class is an interface
SpringMVC - Failed to instantiate Specified class is an interface
494 0
|
Java
【EventBus】Subscribe 注解分析 ( Subscribe 注解属性 | threadMode 线程模型 | POSTING | MAIN | MAIN_ORDERED | ASYNC)
【EventBus】Subscribe 注解分析 ( Subscribe 注解属性 | threadMode 线程模型 | POSTING | MAIN | MAIN_ORDERED | ASYNC)
722 0