有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用EventBus OneEvent方法?

我在Android应用程序中使用EventBus。在我的mainActivity中,我有一个处理程序方法,它将实时数据发送到EventBus,如下所示:

private final Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case TGDevice.MSG_STATE_CHANGE:
                EventBus.getDefault().postSticky(msg.arg1);
                ...

我正在使用Fragments类,需要从处理程序接收消息

我已在onCreateView方法中注册了Fragment类,如下所示:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_data_log, container, false);
    EventBus.getDefault().register(this);
    tv = (TextView) view.findViewById(R.id.tv);
}

public void onEvent(Message message){
    tv.setText("Signal" + message);
}

我有一个onEvent方法,假设在发生事件时调用它。不幸的是,这个方法从未被调用。我认为它可能是被重写的方法,但它似乎不是

要从EventBus读取消息,我需要做什么

另外,在调试模式下,我在哪里可以看到正在创建的线程数?(我正在使用Android Studio)


共 (1) 个答案

  1. # 1 楼答案

    Unfortunately, this method is never called

    这是因为你的onEvent()需要一个Message,并且(大概)你没有发布一个Message。不管arg1是什么,你都在发帖

    Also, in debugging modes, where can I see the number of threads being created? (I'm using Android Studio)

    进入Android Debug Monitor(主菜单中的Tools>;Android>;Android Debug Monitor),DDMS中有一个线程视图