在EventBus的fragments之间没有注册事件类的订阅者
2019-1-28 3:35:4
收藏:0
阅读:84
评论:1
我正在尝试在两个fragments之间使用GreenRobot EventBus,但是我仍然没有注册任何事件类的订阅者。在我的情况下,我在底部导航栏中有两个fragments,没有任何按钮。因此,我点击栏上的图标并更改fragment,然后从第一个fragment EventBus获取字符串并传递给第二个fragment。
第一个fragment(Sender):
private void sendLocation(String location) {
EventBus.getDefault().post(new BusEvent(location));
}
@Override
public void onStop() {
super.onStop();
sendLocation(location);
Log.d("TF ", location);
}
第二个fragment(recipient):
@Subscribe(threadMode = ThreadMode.MAIN)
public void onBusEvent(BusEvent event) {
String location = event.location;
hourlyViewModel.location.set(location);
}
@Override
public void onAttachFragment(@NonNull Fragment childFragment) {
super.onAttachFragment(childFragment);
EventBus.getDefault().register(this);
}
@Override
public void onDetach() {
super.onDetach();
EventBus.getDefault().unregister(this);
}
事件类:
public class BusEvent {
public String location;
public BusEvent(String location) {
this.location = location;
}
public String getLocation() {
return location;
}
}
日志:
D/EGL_emulation: eglMakeCurrent: 0xae414900: ver 3 0 (tinfo 0xaa403f20)
W/art: 在Android 4.1之前,int androidx.appcompat.widget.DropDownListView.lookForSelectablePosition(int, boolean)方法会错误地覆盖android.widget.ListView中的私有方法
I/Choreographer: 跳过39帧!应用程序可能在其主线程上做太多的工作。
D/OkHttp: --> GET http://api.openweathermap.org/data/2.5/weather?q=Rome&appid=52e6ff60bba8613b4850e065dcd3d0ac&units=metric
--> END GET
D/OkHttp: <-- 200 OK http://api.openweathermap.org/data/2.5/weather?q=Rome&appid=52e6ff60bba8613b4850e065dcd3d0ac&units=metric (432ms)
D/OkHttp: 服务器:openresty
日期:2019年1月27日16:10:39 GMT
内容类型:application/json; charset=utf-8
内容长度:432
连接:保持活动状态
X-Cache-Key:/data/2.5/weather?q=rome&units=metric
访问控制允许来源:*
访问控制允许凭证:true
访问控制允许方法:GET、POST
D/OkHttp: {"coord":{"lon":12.48,"lat":41.89},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"base":"stations","main":{"temp":10.28,"pressure":1003,"humidity":76,"temp_min":8,"temp_max":12},"visibility":10000,"wind":{"speed":6.2,"deg":150},"clouds":{"all":75},"dt":1548604200,"sys":{"type":1,"id":6792,"message":0.0036,"country":"IT","sunrise":1548570428,"sunset":1548605946},"id":6539761,"name":"Rome","cod":200}
<-- END HTTP (432-byte body)
D/CWR: androidx.lifecycle.MutableLiveData@228dfe8
W/Glide: 未能找到GeneratedAppGlideModule。你应该在你的应用程序中包括一个annotationProcessor编译依赖于com.github.bumptech.glide:compiler并且一个@GlideModule注释的AppGlideModule实现,否则LibraryGlideModules会被默默地忽略
D/EventBus: 没有为com.example.daniellachacz.weatherapp2.view.BusEvent事件类注册订阅者
D/EventBus: 没有为org.greenrobot.eventbus.NoSubscriberEvent事件类注册订阅者
D/TF: Rome
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

这是因为你没有在片段中调用
register EventBus。你尝试使用 onAttachFragment 注册 EventBus,但它在片段将另一个片段作为其子片段附加时才会被调用。正如文档所说:因此,你需要使用 onAttached。
你的代码应该像这样:
@Override public void onAttach(Context context) { super.onAttach(context); EventBus.getDefault().register(this); } @Override public void onDetach() { super.onDetach(); EventBus.getDefault().unregister(this); }或者像 EventBus 文档 中使用
onStart()和onStop()。