在我的应用程序中,我想使用EventBus,并添加了此依赖关系 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)
我该如何解决?请帮助我
答案 0 :(得分:3)
您错过了onMessageEvent
,请将其添加到Activity
中。这里的MessageEvent
是模式类,与事件一起传递。
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {/* Do something */};
这是一种从触发事件中获取传递数据的方法。