如何使用事件总线?
How to use EventBus?
在我的应用程序中,我想使用 EventBus,我添加了这个 dependency implementation 'org.greenrobot:eventbus:3.1.1'
.
我写了下面的代码,但是当 运行 应用程序显示强制关闭错误并关闭应用程序时!
我的Java代码:
@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);
}
显示此 行 的错误:EventBus.getDefault().register(this);
LogCat 错误:
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)
我该如何解决?请帮助我
您错过了 onMessageEvent
,将此添加到您的 Activity
。这里MessageEvent
是一个模态class,和Event一起传递。
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {/* Do something */};
这是您从触发的事件中获取传递数据的方法。
在我的应用程序中,我想使用 EventBus,我添加了这个 dependency implementation 'org.greenrobot:eventbus:3.1.1'
.
我写了下面的代码,但是当 运行 应用程序显示强制关闭错误并关闭应用程序时!
我的Java代码:
@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);
}
显示此 行 的错误:EventBus.getDefault().register(this);
LogCat 错误:
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)
我该如何解决?请帮助我
您错过了 onMessageEvent
,将此添加到您的 Activity
。这里MessageEvent
是一个模态class,和Event一起传递。
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {/* Do something */};
这是您从触发的事件中获取传递数据的方法。