其他分享
首页 > 其他分享> > android – 简单的EventBus – 没有订阅者注册

android – 简单的EventBus – 没有订阅者注册

作者:互联网

我正在尝试实现EventBus Library for Android的绝对基本实现.

我正在尝试用户在活动1中简单输入内容,然后使用eventbus将整个对象发布到下一个活动 – 活动2.
我完全遵循给定的指导方针:

PART 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: Subscription in the second activity

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());
}

PART 3: Post the event

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

我错过了什么?
当我在同一个类中进行订阅时,它正在工作.

解决方法:

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

标签:android,event-bus,greenrobot-eventbus-3-0
来源: https://codeday.me/bug/20190712/1437768.html