2

I'm implementing EventBus lib to pass and get data from any class or fragment and also subscribed an method to get instant changed data... But I got the following error message:

org.greenrobot.eventbus.EventBusException: Subscriber class java.lang.Boolean and its super classes have no public methods with the @Subscribe annotation

I have subscribed method as github showed here

Code snippet

public class ItemFragment extends Fragment {
    View view;
    String data;
    RecyclerView recyclerView;
    CategoryAdapter itemAdapter;
    List<Category.Items> list;
    private String TAG = getClass().getName();


    public static ItemFragment newInstance(String detail) {

        Bundle args = new Bundle();
        ItemFragment fragment = new ItemFragment();
        args.putString("data", detail);
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.cat_recy, container, false);

        return view;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        data = getArguments().getString("data");



    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getEventBusData(MessageEvent messageEvent) {
        if (messageEvent != null) {
            Category.Items itemClass = EventBus.getDefault().getStickyEvent(Category.Items.class);
            Log.e("eventBus", itemClass.getItem_name() + "  " + itemClass.getPrice() + "  " + itemClass.getCount());
        }

    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}

I have only posted the required code for adopter as the rest of the code is not needed here

adapter

 @Override
        public void onBindViewHolder(final Holder holder, int position) {
            final Category.Items category = list.get(position);
      holder.catPlus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
     EventBus.getDefault().postSticky(category);
    }
    }

update activity class

 @Override
protected void onStop() {
    super.onStop();
    EventBus.getDefault().unregister(this);
}

@Override
protected void onStart() {
    super.onStart();
    EventBus.getDefault().register(this);

}
@Subscribe(threadMode = ThreadMode.MAIN)
public void getEventBusData(Items messageEvent) {
    if (messageEvent != null) {
        Items itemClass = EventBus.getDefault().getStickyEvent(Items.class);
        Log.e("eventBus", itemClass.getItem_name() + "  " + itemClass.getPrice() + "  " + itemClass.getCount());
        calculation();
    }

}

new error

org.greenrobot.eventbus.EventBusException: Subscriber class com.icanstudioz.foodpaprica.fragment.ItemFragment already registered to event class com.icanstudioz.foodpaprica.data.Items
  • The answer to your question is there : https://stackoverflow.com/questions/35874055/eventbus-subscriber-class-and-its-super-classes-have-no-public-methods-with-th/35912503#35912503 – magiccus Aug 01 '17 at 13:02

2 Answers2

9
-keepattributes *Annotation*
-keepclassmembers class ** {
    @org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }
yishan wu
  • 91
  • 1
  • 3
  • 6
    While this code may answer the question, providing information on how and why it solves the problem improves its long-term value. – L_J Aug 12 '18 at 09:22
  • It worked for me. If you are using proguard (if minifyEnabled true) then you have to add this in proguard-rules.pro file. I suppose proguard doesn't keep the annotations by default. – Danil.B Jan 23 '19 at 18:10
0

Please change onStart and onDestroy code by below code:

@Override 
    public void onStart() { 
        super.onStart(); 
        EventBus.getDefault().register(this); 
    } 

    @Override 
    public void onDestroy() { 
        EventBus.getDefault().unregister(this); 
        super.onDestroy(); 
    } 

What I'm doing is, Registering the event to current Class.

For more details, you must refer http://greenrobot.org/eventbus/documentation/how-to-get-started/

Corical
  • 15
  • 5
Anand Singh
  • 5,672
  • 2
  • 23
  • 33
  • @beginner means you tried `this` instead of `true`? Still you get same error? – Anand Singh Aug 02 '17 at 06:13
  • for now i have changed it for now like below but still i have to check this with lifecycle **if (!EventBus.getDefault().isRegistered(this)) { EventBus.getDefault().register(this); }** –  Aug 02 '17 at 06:34
  • Give a try by moving register and unregister code in activity to `onCreate` and `onDestroy` – Anand Singh Aug 02 '17 at 06:37