简体   繁体   中英

How to use EventBus?

In my application i want use EventBus and i added this dependency implementation 'org.greenrobot:eventbus:3.1.1' .

I write below codes, but when run application show me force close error and close application!

My Java codes:

@Subscribe(threadMode = ThreadMode.MAIN)
public void subscribeCancel() {
    prefsUtils.setToShared_BOOL(PrefsKeys.IS_PREMIUM_USER.name(), false);
    navHeader_VipLayout.setVisibility(View.GONE);
    navHeader_notVipLayout.setVisibility(View.VISIBLE);
}

@Override
public void onStart() {
    super.onStart();
    EventBus.getDefault().register(this);
}

@Override
public void onStop() {
    super.onStop();
    EventBus.getDefault().unregister(this);
}

Show me error for this line : EventBus.getDefault().register(this);

LogCat error :

Process: com.app.test, PID: 859
                                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.test/com.app.test.activity.MainActivity2}: org.greenrobot.eventbus.EventBusException: Subscriber class com.app.test.activity.MainActivity2 and its super classes have no public methods with the @Subscribe annotation
                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
                                                                   at android.app.ActivityThread.-wrap14(ActivityThread.java)
                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                   at android.os.Looper.loop(Looper.java:154)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:6776)
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
                                                                Caused by: org.greenrobot.eventbus.EventBusException: Subscriber class com.app.test.activity.MainActivity2 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:140)
                                                                   at com.app.test.activity.MainActivity2.onStart(MainActivity2.java:759)
                                                                   at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1256)
                                                                   at android.app.Activity.performStart(Activity.java:6973)

How can i fix it? please help me

You missed onMessageEvent , Add this to your Activity . Here MessageEvent is an modal class, which is passed with Event.

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

It is the method where you will get passed data from fired events.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM