I used to write eventbus according to the document, but when I tested WeChat payment yesterday, I found that it always appeared when returning
org.greenrobot.eventbus.EventBusException: Subscriber class xxx(自己的类)$1 and its super classes have no public methods with the @Subscribe annotation
When checking, it is found that they are all correct, and the registration place is registered
1. Guess that the reason may be repeated registration, so modify the registration and cancel
if (!EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().register(this);
}
still wrong
The guess is that when jumping or passing the value to the next Activity, the message has been sent before the EventBus is registered, so the above error occurs
2. Solutions
Use sticky sending message, sticky sending message means that if no message is received, the latest sent message will continue to be sent
EventBus.getDefault().postSticky(new Messent("2","",""));
Of course, when receiving, specify the method sticky = true; such as @Subscribs(sticky = true), the above problem can be solved.
But there is still a problem with the operation again, continue to track the problem, it may still be registered again
3. Solutions
It is easy to make mistakes , register in onCreate, and then log out onDestory, so that when the registration jumps to another page and comes back, the registration will be repeated, and an error will be reported
The solution is to register in onStart, and then log out onStop. If you are afraid of repeated registration, you can use the flag
In the Activity you need to pass EventBus.getDefault().postSticky(new Messent("2","",""));
In the Activity interface you need to receive
The Activity interface that needs to receive messages is written as follows
boolean flag= true; @Override protected void onStart() { if(flag){ //4. EventBus registration broadcast (), the parameter is the context. Imported EventBus please look for org.greenrobot // Note: there must be registration Unregister (generally perform the unregister operation in OnDestroy) to prevent memory leaks. Registering an interface can only be registered once, otherwise an error will be reported EventBus.getDefault().register(this); //Change the mark so that it will not be registered again , multiple registrations will report an error flag = false; } super.onStart(); }
Log out in onStop
@Override protected void onStop() { EventBus.getDefault().removeAllStickyEvents(); //解除注册 EventBus.getDefault().unregister(this); super.onStop(); }
Where you need to receive the message write
It should be noted that you must write sticky = true
@Subscribe(threadMode = ThreadMode.MAIN,sticky = true) public void onEvent(Messent messageEvent) { if (messageEvent.getMessage().equals("1")){ Http.getJoin(basicPreferences.getString("userName", ""), "", basicPreferences.getString("userName", ""), id, "android", new Callback.CacheCallback<String>() { @Override public boolean onCache(String result) { return false; } @Override public void onSuccess(String result) { if (result != null && !result.isEmpty()) { JSONObject object = JSON.parseObject(result); if ("success".equalsIgnoreCase(object.getString("flag"))) { } } } @Override public void onError(Throwable ex, boolean isOnCallback) { } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } }); }else{ Toast.makeText(this, "支付失败", Toast.LENGTH_LONG).show(); finish(); } }
finally solve the problem