Android Simple EventBus-未注册订户
我正在尝试为Android实现库的绝对基本实现 我试图通过用户在Android Simple EventBus-未注册订户,android,event-bus,greenrobot-eventbus-3.0,Android,Event Bus,Greenrobot Eventbus 3.0,我正在尝试为Android实现库的绝对基本实现 我试图通过用户在活动1中简单地输入内容,然后使用eventbus将整个对象发布到下一个活动-活动2。 我完全遵循给定的指导原则: 第1部分:POJO 第2部分:第二个活动中的订阅 第三部分:活动结束后 我得到一个错误: D/EventBus: No subscribers registered for event class co.swisdev.abhinav.eventbustesting.StudentEvent D/EventBus: No
活动1
中简单地输入内容,然后使用eventbus将整个对象发布到下一个活动-活动2
。
我完全遵循给定的指导原则:
第1部分:POJO
第2部分:第二个活动中的订阅
第三部分:活动结束后
我得到一个错误:
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
我错过了什么?当我在同一个类中订阅时,它正在工作。这似乎是一个时间问题。活动2必须注册才能接收事件。如果您发布活动1中的事件,则不能保证活动2已创建。虽然这不是答案,但我认为这与EventBus用户有关,我们需要在
onStart()
中注册到EventBus,并在onStop()中注销
如果不将EventBus.getDefault()放入寄存器(此);进入onCreate。onEvent Lisneteners不能着火
activity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_fragment_layout);
EventBus.getDefault().register(this);
}
public void onEvent(AccountSelectedEvent event) {
FragTaxCustomsInfo frag = (FragTaxCustomsInfo) getSupportFragmentManager()
.findFragmentByTag(FragTaxCustomsInfo.TAG);
frag.paymentSelected();
}
public void onEvent(AccountSelectedUpdateIndexEvent event) {
FragTaxCustomsInfo frag = (FragTaxCustomsInfo) getSupportFragmentManager()
.findFragmentByTag(FragTaxCustomsInfo.TAG);
frag.setSelectedPayment(event.getIndex(), false);
}
有同样的问题,也许这会有帮助。正如@JesseBuss所说,这是一个时间问题,因为必须完全创建并注册Activity2
才能接收事件。但是,有一种方法可以确保将对象从Activity1
传输到Activity2
,但需要更多的代码。为此,Activity2
应订阅将由Activity1
发布的事件,Activity1
还应订阅将由Activity2
发布的事件。例如:
活动1
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getDefault().register(this); //register here or onStart, etc
}
private void startActivity2(){//Start activity2 without any extras
//Intent Activity2
//startActivity(Activity2)
}
@Subscribe
public void eventFromActivity2(String fromActivity2){
//subscribe to an event to be posted by activity2 (e.g a String).
//At this point Activity2 should be fully created to respond to events from Activity1
StudentEvent studentEventObject = new StudentEvent(
etRegistrationNumber.getText().toString(),
etName.getText().toString(),
etCourse.getText().toString(),
etBranch.getText().toString()) ;
EventBus.getDefault().post(studentEventObject);//post event back to Activity2
EventBus.getDefault().unregister(this);//unregister
}
活动2
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getDefault().register(this);//register here
// after completing onCreate, post event to Activity1
EventBus.getDefault().post("to Activity1");
}
protected void onDestroy() {
EventBus.getDefault().unregister(this); //unregister here
super.onDestroy();
}
@Subscribe
public void eventReceiver(StudentEvent studentEvent){ //respond to event from Activity1
tvRegistrationNumber.setText(studentEvent.getRegistrationNumber());
tvName.setText(studentEvent.getName());
tvBranch.setText(studentEvent.getBranch());
tvCourse.setText(studentEvent.getCourse());
}
另外,请确保从事件中正确注销您应该尝试在“活动2”中使用@Subscribe(sticky=true)
注释,并在“活动1”中调用bus.poststick(…)
方法
您必须使用sticky事件,因为您正在发布StudentEvent,但尚未找到订户,因为订户活动尚未创建。若您使用PostStick,那个么事件总线将在内存中保存此事件,并在将来发现订户时传递它
请参见以下示例:
第1部分:
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;
}
}
第2部分:以粘性订阅
EventBus.getDefault().register(this); //onCreate
EventBus.getDefault().unregister(this); //onDestroy
@Subscribe(sticky = true)
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().postSticky(studentEventObject);
祝你好运 答案是正确的,但要想成为一个好答案,还需要进一步充实。您可能希望指向“活动生命周期”,并解释为什么这甚至不是EventBus的正确应用程序。那么,我必须做的最佳修改是什么?我是否应该制定意图,然后将事件发布到EventBus@323go@AbhinavJordiieDas这不是EventBus通常用于的类型。通常,EventBus用于接收已激活活动或片段的更新数据。将EventBus更多地看作是观察者/消费者的包装器。对于本例,您似乎正在使用它将数据传递给另一个活动。我认为您最好使用标准方法,使用捆绑包,或将其保存到模型并在活动2中访问该模型。@JesseBuss事件总线是否可以替代?@Abhinavjordieda,因为它们似乎为我做了不同的事情。包裹器使向新活动传递数据变得更容易。我认为在这个例子中,包裹员将是一个很好的选择!现在让我们假设您有一个网络请求来获取最新的StudentEvent。我们启动活动2,几分钟后我们得到新的StudentEvent。现在是发布新的StudentVentrived事件的时候了,以提醒活动有新的数据可用。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getDefault().register(this);//register here
// after completing onCreate, post event to Activity1
EventBus.getDefault().post("to Activity1");
}
protected void onDestroy() {
EventBus.getDefault().unregister(this); //unregister here
super.onDestroy();
}
@Subscribe
public void eventReceiver(StudentEvent studentEvent){ //respond to event from Activity1
tvRegistrationNumber.setText(studentEvent.getRegistrationNumber());
tvName.setText(studentEvent.getName());
tvBranch.setText(studentEvent.getBranch());
tvCourse.setText(studentEvent.getCourse());
}
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;
}
}
EventBus.getDefault().register(this); //onCreate
EventBus.getDefault().unregister(this); //onDestroy
@Subscribe(sticky = true)
public void eventReceiver(StudentEvent studentEvent){
tvRegistrationNumber.setText(studentEvent.getRegistrationNumber());
tvName.setText(studentEvent.getName());
tvBranch.setText(studentEvent.getBranch());
tvCourse.setText(studentEvent.getCourse());
}
StudentEvent studentEventObject = new StudentEvent(
etRegistrationNumber.getText().toString(),
etName.getText().toString(),
etCourse.getText().toString(),
etBranch.getText().toString()) ;
EventBus.getDefault().postSticky(studentEventObject);