使用EventBus框架报异常:its super classes have no public methods with the @Subscribe annotation

来源:互联网 发布:python help 函数 编辑:程序博客网 时间:2024/06/04 19:49

今天看了旗舰的博客http://blog.csdn.net/harvic880925/article/details/40660137中关于EventBus框架的介绍,然后自己就去依葫芦画瓢的敲了一下代码,等到运行的时候报异常了

 Caused by: org.greenrobot.eventbus.EventBusException: Subscriber class com.wenxia.testdemo.EventBusFirstActivity and its super classes have no public methods with the @Subscribe annotation

然后又看了一下https://github.com/greenrobot/EventBus中的集成步骤

1.定义事件

public static class MessageEvent { /* Additional fields if needed */ }

2.准备订阅者:声明和批注你订阅的方法,可以选择指定线程模式

@Subscribe(threadMode = ThreadMode.MAIN)  public void onMessageEvent(MessageEvent event) {/* Do something */};

注册和注销您的订阅服务器。例如在安卓系统,活动和碎片应该通常注册根据他们的生命周期︰

@Overridepublic void onStart() {    super.onStart();    EventBus.getDefault().register(this);}@Overridepublic void onStop() {    super.onStop();    EventBus.getDefault().unregister(this);}

3.传递事件

EventBus.getDefault().post(new MessageEvent());

原来是要在onMessageEvent(MessageEvent event)方法上面加上@Subscribe,然后再次运行就没问题了

0 0
原创粉丝点击