微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐