我正在尝试使用EventBus
将文本从我的tabLayout
发送到Fragment
,但是由于某些未知原因,该事件并未触发。我在堆栈跟踪中收到此错误。我不明白为什么未注册订户。
D/EventBus: No subscribers registered for event class org.greenrobot.eventbus.NoSubscriberEvent
Fragment.java
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
@Subscribe
public void receivedQueryMessage(BusMessage message){
Log.d("MYLOG", "message");
}
MainActivity.java
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
if (tab.getText() != null) {
EventBus.getDefault().post(tab.getText());
}
}
BusMessage.java
public class BusMessage {
private String message;
public BusMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}
答案 0 :(得分:1)
EventBus.getDefault().post(new BusMessage(tab.getText()));