EventBus 中的片段之间没有注册事件 class 的订阅者

No subscribers registered for event class between fragments in EventBus

我正在尝试在两个片段之间使用 GreenRobot EventBus,但我仍然没有为事件 class 注册的订阅者。就我而言,底部导航栏中有两个片段,没有任何按钮。所以我单击栏上的图标并更改片段,然后 EventBus 从第一个片段获取字符串并传递给第二个片段。

第一个片段(发件人):

private void sendLocation(String location) {
    EventBus.getDefault().post(new BusEvent(location));
}

@Override
public void onStop() {
    super.onStop();
    sendLocation(location);
    Log.d("TF ", location);
}

第二个片段(收件人):

@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: Before Android 4.1, method int androidx.appcompat.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
I/Choreographer: Skipped 39 frames!  The application may be doing too much work on its main thread.
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: Server: openresty
    Date: Sun, 27 Jan 2019 16:10:39 GMT
    Content-Type: application/json; charset=utf-8
    Content-Length: 432
    Connection: keep-alive
    X-Cache-Key: /data/2.5/weather?q=rome&units=metric
    Access-Control-Allow-Origin: *
    Access-Control-Allow-Credentials: true
    Access-Control-Allow-Methods: 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: Failed to find GeneratedAppGlideModule. You should include an annotationProcessor compile dependency on com.github.bumptech.glide:compiler in your application and a @GlideModule annotated AppGlideModule implementation or LibraryGlideModules will be silently ignored
D/EventBus: No subscribers registered for event class com.example.daniellachacz.weatherapp2.view.BusEvent
D/EventBus: No subscribers registered for event class org.greenrobot.eventbus.NoSubscriberEvent
D/TF: Rome

这是因为你没有在你的Fragment中调用register EventBus。您正在尝试使用 onAttachFragment 注册 EventBus,但在该片段附加另一个片段作为其子片段之前不会调用它。正如文档所说:

public void onAttachFragment (Fragment childFragment) Called when a fragment is attached as a child of this fragment.

This is called after the attached fragment's onAttach and before the attached fragment's onCreate if the fragment has not yet had a previous call to onCreate.

因此,您需要改用 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 documentation.

中那样使用 onStart()onStop()