在我的应用程序中,我希望使用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)我怎么才能修好它?请帮帮我
发布于 2018-07-16 13:42:30
您错过了onMessageEvent,将其添加到Activity中。这里,MessageEvent是一个模态类,它与事件一起传递。
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {/* Do something */};它是从激发事件中获得传递数据的方法。
https://stackoverflow.com/questions/51363180
复制相似问题