EventBus使用中遇到的问题

1.在订阅的页面onCreate中进行注册 

EventBus.getDefault().register(this);

2.同时在onDestory中解除注册

@Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
3.重写event(onEvent、 onEventMainThread、onEventAsync、onEventBackgroundThread)

4.在需要的地方发布订阅

EventBus.getDefault().post(new FirstEvent("success"));

EventBus 3.0可能出现异常

FATAL EXCEPTION: main
                                                                   Process: com.hank.rxjavademo, PID: 6329
                                                                   org.greenrobot.eventbus.EventBusException: Subscriber class com.hank.fragment.map.MapFragment 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:136)
                                                                       at com.hank.fragment.map.MapFragment.onCreateView(MapFragment.java:34)



解决方案:

在重写的event方法前加注解 @Subscribe

@Subscribe
    public void onEventMainThread(FirstEvent event){
        eventBusButton.setText(event.getMsg());
    }



  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值