0

I am experimenting EventBus libaray. I just registered it into Fragment and I used @Subcride annotation into that fragment with public methods but when I run the app I get exception which tells that I don't have used @Subcribe annotation with public methods in super class. Why do I need to declare it in super class? How to fix this problem This is my first time with EventBus

Here is code

Fragment

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());
}
}

This is the exception I get

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)
Lahiru Ashan
  • 767
  • 9
  • 16
Zeeshan Shabbir
  • 6,704
  • 4
  • 38
  • 74

3 Answers3

2

Don't pass getActivity() to EventBus register() method, pass the Fragment instead, which in this case should be this

elmorabea
  • 3,243
  • 1
  • 14
  • 20
0

You are likely calling EventBus.getDefault().register(this) in your activity but do not have any methods with the @Subscribe annotation.

You only need to register the components that use the annotations.

Move the registration to your Fragment.

Kuffs
  • 35,581
  • 10
  • 79
  • 92
  • I am doing all this stuff in fragment since i want all this functionality in fragment – Zeeshan Shabbir Oct 31 '16 at 06:22
  • The exception states you have registered your activity as well as\instead of the fragment. If you have no @Subscrive annotations in the activity, you should not register it with the event bus – Kuffs Oct 31 '16 at 06:22
  • What I said was correct but on checking further, you are using OnAttach in your Fragment to register the Activity. Please use OnPause/OnResume to register and unregister the Fragment instead. – Kuffs Oct 31 '16 at 06:30
  • Thanks. It was happening because of ```getActivity``` as parameter to ```register()```. I changed it to ```EventBus.getDefault().register(this)``` inside fragment and this worked. – Zeeshan Shabbir Oct 31 '16 at 07:10
0

Consider using sticky events (registerSticky()), because events sent this way will wait until fragment receive them. This can be helpful to metigate Fragment's lifecycle. - Activity send event at a time when fragment not ready and fragment will receive it at some point - when it inflated and ready. For subscribing to some kind of event create public method with @Subscribe annotation.

Alex Shutov
  • 3,217
  • 2
  • 13
  • 11