如何将EventBus设置为片段

时间:2016-10-31 06:10:37

标签: java android android-fragments exception greenrobot-eventbus

我正在试验EventBus libaray。我刚刚将它注册到Fragment中,并使用@Subcride方法将public注释用于该片段,但是当我运行应用程序时,我得到异常,它告诉我没有使用@Subcribe注释public类中的super个方法。为什么我需要在super课程中声明它?如何解决这个问题 这是我第一次使用EventBus

这是代码

片段

public class SignUpFragment extends BaseFragment {
Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View layout = inflater.inflate(R.layout.fragment_sign_up, container, false);
    ButterKnife.bind(this, layout);
    return layout;
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(String uri) {
    if (mListener != null) {
        mListener.onSignUpFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    EventBus.getDefault().register(getActivity()); // Here comes the exception
    if (context instanceof OnSignUpFragmentInteractionListener) {
        mListener = (OnSignUpFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnSignUpFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    EventBus.getDefault().unregister(getActivity());
    mListener = null;
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(JSONObject response) {

    Log.d(TAG, response.toString());

}

@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(VolleyError error) {
    Log.d(TAG, error.toString());
}
}

这是我得到的例外

10-31 11:01:46.172 9047-9047/com.aam.skillschool E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.aam.skillschool, PID: 9047
org.greenrobot.eventbus.EventBusException: Subscriber class com.aam.skillschool.ui.activities.MainActivity and its super classes have no public methods with the @Subscribe annotation
at org.greenrobot.eventbus.SubscriberMethodFinder.findSubscriberMethods(SubscriberMethodFinder.java:67)
at org.greenrobot.eventbus.EventBus.register(EventBus.java:136)
at com.aam.skillschool.ui.fragments.SignUpFragment.onAttach(SignUpFragment.java:113)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1048)
at android.support.v4.app.BackStackRecord.setLastIn(BackStackRecord.java:838)
at android.support.v4.app.BackStackRecord.calculateFragments(BackStackRecord.java:878)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:719)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1682)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:541)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

3 个答案:

答案 0 :(得分:2)

不要将getActivity()传递给EventBus register()方法,而是传递片段,在这种情况下应该是this

答案 1 :(得分:0)

您可能在活动中呼叫EventBus.getDefault().register(this),但没有@Subscribe注释的任何方法。

您只需注册使用注释的组件。

将注册移至Fragment

答案 2 :(得分:0)

考虑使用粘性事件(registerSticky()),因为以这种方式发送的事件将一直等到片段接收它们。这有助于改善Fragment的生命周期。 - 当片段未准备好时,活动发送事件和片段将在某个时刻接收它 - 当它充气并准备好时。 对于订阅某种事件,使用@Subscribe注释创建公共方法。