我正在尝试为 Android 实现EventBus库的绝对基本实现。
我试图通过用户输入简单的内容,activity 1
然后我使用 eventbus 将整个对象发布到下一个活动而不是意图附加内容 - activity 2
。我完全遵循给定的指导方针:
第 1 部分:POJO
public class StudentEvent {
public final String registrationNumber ;
public final String name ;
public final String course ;
public final String branch ;
public StudentEvent(String registrationNumber, String name, String course, String branch) {
this.registrationNumber = registrationNumber;
this.name = name;
this.course = course;
this.branch = branch;
}
public String getRegistrationNumber() {
return registrationNumber;
}
public String getName() {
return name;
}
public String getCourse() {
return course;
}
public String getBranch() {
return branch;
}
}
PART 2:第二个活动的订阅
EventBus.getDefault().register(this); //onCreate
EventBus.getDefault().unregister(this); //onDestroy
@Subscribe
public void eventReceiver(StudentEvent studentEvent){
tvRegistrationNumber.setText(studentEvent.getRegistrationNumber());
tvName.setText(studentEvent.getName());
tvBranch.setText(studentEvent.getBranch());
tvCourse.setText(studentEvent.getCourse());
}
第 3 部分:发布活动
StudentEvent studentEventObject = new StudentEvent(
etRegistrationNumber.getText().toString(),
etName.getText().toString(),
etCourse.getText().toString(),
etBranch.getText().toString()) ;
EventBus.getDefault().post(studentEventObject);
我得到错误:
D/EventBus: No subscribers registered for event class co.swisdev.abhinav.eventbustesting.StudentEvent
D/EventBus: No subscribers registered for event class org.greenrobot.eventbus.SubscriberExceptionEvent
我错过了什么?
当我在同一个班级订阅时它正在工作。