订阅者未从eventBus获得发布的消息

订阅者未从eventBus获得发布的消息

本文介绍了订阅者未从eventBus获得发布的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中使用事件总线.为此,我将eventbus lib添加到了gradle中.然后,我创建了一个像这样的类:

I want to use eventbus into my app. for that I add eventbus lib to my gradle.then I have created a class like this:

public class UserEvent {

private User eUser; // esup user model
private String domain;
private String securitySrv;
private Cookie cookie;
private int totalUsersSecurity;
private ExecutorService executorService;
// constructor and getter

然后在主要片段中添加以下行,最后我发布了消息:

then into main fragment I have added this lines and finally I have posted my message:

UserEvent userEvent = new UserEvent(eUser, domain, securitySrv, cookie,
totalUsersSecurity, executorService);
EventBus.getDefault().post(userEvent);

进入主片段,我有ViewPager,该传呼器已经加载了另一个名为 AllUsersFragment 的片段.

into main fragment I have ViewPager that this pager have loaded another fragment called AllUsersFragment.

首先在AllUsersFragment中注册了事件总线:

In AllUsersFragment first I have registered eventbus:

EventBus bus = EventBus.getDefault();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_layout, container, false);
    bus.register(this);
    return rootView;
}

然后我写了这个方法:

    @Subscribe
public void onEvent(UserEvent event) {
    securitySrv = event.getSecuritySrv();
    cookie = event.getCookie();
    totalUsersSecurity = event.getTotalUsersSecurity();
    executorService = event.getExecutorService();

}

进入我的简历方法中,我想通过其构造函数将UserEvent从eventbus传递到另一个类:

into my resume method I want to pass UserEvent from eventbus to another class by its constructor:

@Override
public void onResume() {
    super.onResume();
    paginator = new Paginator(getContext(),totalUsersSecurity,executorService);

}

但是totalUsersSecurity和executorService为空吗?虽然这些变量是在主要片段中填充的.在android studio的调试模块中,从未调用过public void onEvent(UserEvent event).谢谢你的帮助?

but totalUsersSecurity and executorService are null? while these variables was fill at main fragment.in debug mod at android studio this public void onEvent(UserEvent event) is not never called.thank you for helping?

我正在将Android Studio 3与gradle:3.0.0'一起使用

I am using android studio 3 with gradle:3.0.0'

推荐答案

最后,我解决了这个问题.根据 eventBus Wiki

Finally I fix this problem. according eventBus wiki

我只使用 Sticky Events .现在我收到了帖子

I just use Sticky Events .now I received my post

这篇关于订阅者未从eventBus获得发布的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 05:42