5

我正在尝试为 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

我错过了什么?
当我在同一个班级订阅时它正在工作。

4

6 回答 6

13

虽然这不是答案,但我认为这与 EventBus 用户有关。根据 EventBus 文档,我们需要在 EventBus 中onStart()注册和取消注册onStop()

@Override
public void onStart() {
    super.onStart();
    EventBus.getDefault().register(this);
}

@Override
public void onStop() {
   EventBus.getDefault().unregister(this);
    super.onStop();
}
于 2016-07-09T09:23:57.857 回答
11

这似乎是一个时间问题。必须注册活动 2 才能接收事件。如果您从活动 1 发布事件,则不能保证活动 2 已创建。

于 2016-04-17T05:20:45.573 回答
5

您应该尝试@Subscribe(sticky = true)在“活动 2”中使用注释,并bus.postSticky(...)在“活动 1”中调用方法。

文档

于 2018-03-08T21:34:11.237 回答
2

您必须使用粘性事件,因为您正在发布 StudentEvent 但尚未找到订阅者,因为订阅者活动尚未创建。如果您使用 postSticky,则事件总线将在内存中保存此事件,并在将来找到订阅者时传递它。

请参见以下示例:

第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 部分:发布 Sticky 事件

 StudentEvent studentEventObject = new StudentEvent(
        etRegistrationNumber.getText().toString(),
        etName.getText().toString(),
        etCourse.getText().toString(),
        etBranch.getText().toString()) ;

 EventBus.getDefault().postSticky(studentEventObject);

祝你好运...!

于 2018-11-09T07:03:09.333 回答
1

有同样的问题,也许这可能会有所帮助。这是@JesseBuss 所述的时间问题,因为Activity2必须完全创建并注册才能接收事件。但是,有一种方法可以确保将对象从 传输Activity1Activity2,但需要更多代码。为此,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());
}

还要确保正确地从事件中注销

于 2018-02-14T11:20:47.107 回答
0

如果不放 EventBus.getDefault().register(this); 进入 onCreate。onEvent 监听器不能着火。

活动.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);
    }
于 2017-11-03T06:28:42.073 回答